1
私はeditTextとrecyclerViewで作業しています。 EditTextに文字を書くと、私のrecyclerViewが更新されます。漏出:Timer and TextWatcher
ユーザーが文字を書くたびにリクエストを送信しないように、私はTextWatcherの中にTimerを配置しました。
searchDestinationEt.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {
//There is nothing to do here
}
@Override
public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
if (timer != null) {
timer.cancel();
}
}
@Override
public void afterTextChanged(final Editable s) {
timer = new Timer();
//we schedule this in order to avoid sending useless request.
//We wait the user is finishing writing before sending requests
timer.schedule(new TimerTask() {
@Override
public void run() {
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
actionsListener.onDestinationSearch(s.toString());
}
});
}
}, DELAY_SEND_REQUEST);
}
});
これはうまく動作しますが、リークキャナリーによれば、このコード部分にはリークがあります。
http://www.mopri.de/2010/timertask-bad-do-it-the-android -way-use-a-handler/ – oiZo
@oiZo Thx、私は試してみます。しかし、そこに漏れがある理由を説明していません。 – Bob