I have some activities(Say A & B), And from one activity I am calling another activity by using Handler-post Delayed method.
My logic is in App starts with Activity A,and after 3 seconds goes to Activity B. & After 3 seconds, it is working perfectly.
The problem is I have set a time delay of three seconds.And During these three seconds , If I click home button , It goes background and immediately after the specified three seconds of time the application is coming back to the foreground and showing the Next Activity.
try {
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
finish();
Intent i = new Intent(Asplash.this, Example.class);
i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(i);
}
}, 3000);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
handler.removeCallbacksAndMessages(null);
finish();
return super.onKeyDown(keyCode, event);
}
I am using code like this , and for back button during the HANDLER TIME is working fine and the application is completely going background. , But if I press HOME button, it initially goes background and after the completion of HANDLER TIME(3000), application is coming to fore ground. I want it to be in background only after I press Home button also.
Please suggest me>
You just put this in your onPause()
method of your activity:
@Override
public void onPause() {
handler.removeCallbacksAndMessages(null);
super.onPause();
}
So when your app goes to the background, it will remove the callbacks on the Handler. This also works for the back button AND the home button.