當我返回的到上個頁面的時候 發現 時間沒有從新計時。ide
@Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); FragmentManager fm = getFragmentManager(); System.out.println("" + fm); fm.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() { @Override public void onBackStackChanged() { if (BaseFragment.this.isVisible()) { startTimer(); } else { stopTimer(); } } }); }
protected int startTimer() { recLen = 60; callbackTime(recLen); mhandler.removeMessages(RUNNER_MSG_CODE); Message msg = Message.obtain(); msg.what = RUNNER_MSG_CODE; msg.obj = runnable; mhandler.sendMessageDelayed(msg, 1000); // Log.e("startTimer","startTimer"); return recLen; } private void stopTimer() { mhandler.removeMessages(RUNNER_MSG_CODE); recLen = -1; // Log.e("stopTimer","stopTimer"); } public void reStartTimer() { // Log.e("reStartTimer","reStartTimer"); recLen = 60; } private Handler mhandler = new Handler(Looper.getMainLooper()) { @Override public void handleMessage(Message msg) { if (msg.what == RUNNER_MSG_CODE) { this.post((Runnable) msg.obj); } } }; Runnable runnable = new Runnable() { @Override public void run() { recLen--; // cd_time.setText(recLen+""); callbackTime(recLen); // Log.e("cd_time",recLen+"="); if (recLen == 0) { mhandler.removeMessages(RUNNER_MSG_CODE); // backOnce(); start(MainFragment.newInstance()); // Log.e("BaseFragment","BaseFragment=="+recLen); } else if (recLen > 0) { Message msg = Message.obtain(); msg.what = RUNNER_MSG_CODE; msg.obj = runnable; mhandler.sendMessageDelayed(msg, 1000); // Log.e("BaseFragment","BaseFragment=="+recLen); } else if (recLen < 0) { mhandler.removeMessages(RUNNER_MSG_CODE); // Log.e("BaseFragment","BaseFragment=="+recLen); } } };