Reputation: 14330
I have many activities in my application(eg A->B->C->D)...i have a countdowntimer that i am using for a session timeout.. What i have done is created a static counter...I start the counter at activity A....if the user interacts the counter is reset...this also applies for activities B,C,D....also on finishing activity D, Activity A gets started..For this i have used addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
so it clears the stack...
but what happens is when the activity A gets started again..A new instance gets created along with the previous counter and keeps running in background....And is not reset on userinteration... i have done counter = null
in onDestroy....is it right or i need to do something else???
public class CountTime extends Activity {
TextView tv;
static MyCount counter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv = new TextView(this);
this.setContentView(tv);
// 5000 is the starting number (in milliseconds)
// 1000 is the number to count down each time (in milliseconds)
counter = new MyCount(5000, 1000);
counter.start();
}
@Override
public void onUserInteraction() {
// TODO Auto-generated method stub
super.onUserInteraction();
counter.start();
}
// countdowntimer is an abstract class, so extend it and fill in methods
public class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
tv.setText("done!");
}
@Override
public void onTick(long millisUntilFinished) {
tv.setText("Left: " + millisUntilFinished / 1000);
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
counter = null;
}
}
Upvotes: 0
Views: 153
Reputation: 14472
This is normal Android behaviour.
At any time an activity is not the foreground activity, it may be destroyed depending on some non-deterministic criteria such a free memory, how many background apps are running etc.
In your case, Activity A is being destroyed and when you return to it, a new instance is created and it's onCreate() is called.
In general, you should never try to access something inside an activity from another activity.
You could extend your application class and instantiate your timer in that.
public class MyApplication extends Application{
static MyCount counter;
@Override public void onCreate ()
{
super.onCreate();
counter = new MyCount();
}
}
In your manifest
<application android:name="com.myname.MyApplication"
You should only use this technique when it's really needed. Please don't put everything in your extended application class to make things easy. It's really bad practice.
Finally, there is no contract with Android that onDestroy() will be called if your activity is destroyed. It's only guaranteed if you finish() the activity yourself. You should use onPause() to do stuff that needs to happen when your activity goes to the background (and may be destroyed).
Upvotes: 2
Reputation: 9217
You can try this by replacing this line
counter = null;
to the line
counter.cancel();
Upvotes: -1