1

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>

sai
  • 548
  • 10
  • 30
  • You can't catch the `HOME` button, because the user HAS to be able to get out of the application. Otherwise, you could write an app that did nothing on back button and on home button, making you stuck in the app. – tolgap Mar 06 '13 at 09:30

2 Answers2

4

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.

tolgap
  • 9,629
  • 10
  • 51
  • 65
  • I have used something like this quite extensively and it works very well. – ScouseChris Mar 06 '13 at 09:39
  • Exactly , I used it previously, But I used it with comination of onReume() ... also that's why it messed up. Thanks ..... Also One more problem, please during this screen launch of 3 seconds , If I cahnge my Device orientation, the app is closing permanently.Any Ideas?????????/ – sai Mar 06 '13 at 09:49
  • 1
    You can look at this: [Changing orientation and not restarting activity](http://stackoverflow.com/questions/10235178/change-layouts-but-not-restart-activity-on-my-android-app). Android kills and restarts the activity if you don't tell it in the manifest what to do on an `orentationChange`. – tolgap Mar 06 '13 at 10:12
1

You shold start handler from onResume() method like:

@Override
public void onResume() {
    try {
         handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                finish();
                Intent i = new Intent(Asplash.this, Example.class);
                i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                startActivity(i);
           }
         }, 3000);

       } catch (Exception e) {
           e.printStackTrace();
       }
}

And remove handle in onPause() Like :

@Override
public void onPause() {
    handler.removeCallbacksAndMessages(null);
    super.onPause();
}

This will make sure to start other activity, even if you press home within 3 second

Rakesh Bhalani
  • 668
  • 4
  • 10
  • But, It is the Default screen on start of the app , so I put in onCreate(..).From this it will got to next activity. Is it possible with your approach? – sai Mar 06 '13 at 10:06