0

Is there any way to prevent executing code within onResume when returning to an application after the home button has been pressed?
What method is called when the home button is pressed? I could possibly flag something up when home button is pressed?

Thanks

Paul Alexander
  • 2,686
  • 4
  • 33
  • 69
  • What exactly is your requirement? Can you explain in detail? – V_J Sep 24 '15 at 10:34
  • I basically don't wan't the code that I have in onResume to execute after the user has returned to the application after pressing the home button. If I could make boolean executeResumeCode = false; when the home button is pressed and then put a condition to check this when resume is called, I think that would work – Paul Alexander Sep 24 '15 at 10:38

4 Answers4

2

After overriding above method, now you can easily listen HOME Key press in your activity using onKeyDown() method.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {     

    if(keyCode == KeyEvent.KEYCODE_HOME)
    {
       //The Code Want to Perform. 
    }
});

Hope this will help

Sukhbir
  • 1,410
  • 14
  • 33
  • Hi thanks for your reply but it looks like this has been removed after Anroid 4.0 :( – Paul Alexander Sep 24 '15 at 10:50
  • 1
    but You can do this by checking current running applications in Android. you can say that if yours is the recent task that is shown on long pressing the home button, then it was send to background. So, In your onPause() of the activity where you are trying to detect the home button pressed, you can check whether the application has been sent to background. @Override public void onPause() { if (isApplicationSentToBackground(this)){ // Do what you want to do on detecting Home Key being Pressed } super.onPause(); } – Sukhbir Sep 24 '15 at 11:21
2

Thanks for the help everyone

I managed to solve this by creating a boolean, executeOnResume, which I make false everytime onStop() is called and the app is closed. I then check the state of this boolean in onResume() when the app is opened again to choose whether some code should be executed or not.

onStop()

 //-----application stopped
    @Override
    protected void onStop() {
        super.onStop();

        //do not execute onResume code when app opened again
        executeOnResume = false;
    }



onResume()

//-----application resumed
    @Override
    protected void onResume() {
        super.onResume();

        //if true
        if (executeOnResume) {

            //do something
        }

        else {

            //execute onResume true code the next time onResume is called. For example, returning from another activity

        }
    }
Paul Alexander
  • 2,686
  • 4
  • 33
  • 69
0

tapping the Home button creates an intent to launch the Home screen and then starts that inten Correct.

If this is the case, I'd expect the onCreate() method to be run whenever the Home screen is created Not necessarily. If it is already running, it would be called with onNewIntent().

If someone could just offer some enlightenment into this matter, the basic question is whether onResume() or onCreate() gets called when I tap the Home button Any time any activity returns to the foreground from a user input standpoint, onResume() is called. Home screens should be no different in this regard.

onCreate() is called when the activity is created. Existing activities are not created, but are merely brought back to the foreground. If what triggered the activity to return to the foreground was a startActivity() call, the activity will be called with onNewIntent() and onResume() (and usually onStart(), for that matter).


Reference : Which method is run when Home button pressed?

Community
  • 1
  • 1
KishuDroid
  • 5,411
  • 4
  • 30
  • 47
0

Users can leave your app in all kinds of different ways. Pressing HOME is only one of them. An incoming phone call will leave your app, pulling down the list of notifications and pressing one will leave your app, etc. In all of these cases, when the user returns to your app, onResume() will be called. This is standard Android behaviour and tells your app that it is now in the foreground and visible to the user.

Your architecture is flawed if you need to know how the user is returning to your app. You probably need to move some code that you have in onResume() back to onCreate() or onNewIntent().

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • Thanks. I know this. onResume() is called after onCreate() and everytime that the user returns to the app but this wasn't my question. My question was how can I prevent some code being called by onResume() when the user returns to the app after pressing the home button, the solution being that onStop() is called when the home button is pressed (or the application is closed in any other way as you stated), therefore if I change a boolean value to false when onStop is called and check that boolean in onResume when the app is reopened, I'll be able to tell onResume to execute some code or not – Paul Alexander Sep 24 '15 at 11:27
  • Um...not really. If your activity starts another activity, `onStop()` will also be called. So I don't really see how this helps you. – David Wasser Sep 24 '15 at 16:30