0

Consider I have a TimerTask in my Activity for a ticker in UI. I get the value of a TextView every time Timer is called and increment it by 1.What is the expected behavior in UI if onPause and onResume is not implemented and I am going background and coming back. Will the value get updated while I am in background?

Sample code

public class home extends Activity {

private android.os.Handler mTimerHandler = new android.os.Handler();
private Timer mTimer;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    mTimer = new Timer();
    mTimer.schedule(getTimer(), 1, 1000);
}
private TimerTask getTimer() {

    final TextView counterView = (TextView) findViewById(R.id.counter);

    return new TimerTask() {

        public void run() {
            mTimerHandler.post(new Runnable() {
                public void run() {
                    long currentValue = Long.parseLong(counterView.getText().toString());
                    counterView.setText(String.valueOf(currentValue));
                }
            });
        }
    };
}

}

lintu
  • 1,092
  • 11
  • 24
  • **"What is the expected behavior..."** Sorry - I don't do mind-reading on a Thursday. Please post your code. – Squonk Mar 20 '14 at 09:22
  • Sorry for that. I editted the question. – lintu Mar 20 '14 at 09:31
  • 1
    You still haven't posted any code. Using things like a `BroadcastReceiver` and `TimerTask` when an `Activity` is invisible (certainly paused, probably stopped) may or may not work. Without code, nobody can answer your question. It may work as expected or it could crash and burn. There's also a possibility the `Activity` may be destroyed if system resources are low and it will then be GC'd. – Squonk Mar 20 '14 at 09:50
  • I updated the question and added sample code. – lintu Mar 20 '14 at 12:28
  • https://stackoverflow.com/a/58504433/5219642 for more detail you can check this – Amit Oct 25 '21 at 11:55

1 Answers1

1

I have a BroadcastReceiver and TimerTask in onCreate (both updates UI). I am curious to know what is the expected behavior if onPause and onResume is not implemented and I am going background and coming back.

The callbacks are implemented. You usually override and call the super (otherwise an exception will be thrown). It will happen nothing. The super.onResume/super.onPause will be called as usual. What will happen to your BroadcastReceiver depends on your implementation

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • So if I am updating a counter every second in UI through TimerTask. When I come back the value will be proper? – lintu Mar 20 '14 at 09:21
  • how am I suppose to answer without seeing your code? – Blackbelt Mar 20 '14 at 09:21
  • @blackbelt : You already posted an answer without seeing any of the OP's code. – Squonk Mar 20 '14 at 09:27
  • @Squonk, I do think that my answer fulfil his question. At least this part where he sasy: `What is the expected behavior if onPause and onResume is not implemented and I am going background and coming back`. – Blackbelt Mar 20 '14 at 09:30
  • I updated the code and added sample code. I did't had code at the time of asking the question. It was only my curiosity. – lintu Mar 20 '14 at 12:30