Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which method is run when Home button pressed?

I have a Home replacement Activity from within which you can launch a number of apps. When you tap the Home button, you are returned to my Home replacement Activity.

As I understand, tapping the Home button creates an intent to launch the Home screen and then starts that intent (I might be wrong, please correct me if I am!). If this is the case, I'd expect the onCreate() method to be run whenever the Home screen is created. On the other hand, when you launch another activity, the Home screen invokes onPause(). So returning to it makes me assume onResume() is invoked.

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, but additional details are welcome, I'm working on stuff that utilizes this heavily and want to know a lot about it.

like image 766
mike Avatar asked Jul 09 '13 15:07

mike


People also ask

Which method gets called when Home button pressed?

onCreate() is called when the activity is created. Existing activities are not created, but are merely brought back to the foreground.

Which method is called when home button is pressed in Android?

When you press the Home button on the device, the onPause method will be called. If the device thinks that it needs more memory it might call onStop to let your application shut down.

What happens to activity when home button is pressed?

When Home button is pressed, onStop method is called in your activity. So what you may do is to add finish(); in onStop method to destroy your activity. Eventually onDestroy method will be raised to confirm that your activity is finished.

Which lifecycle method will be called when the home button is pressed when you are interacting with the activity?

onPause() is called when Home button is pressed.


3 Answers

When you install application first time following method call one by one in Activity

  1. onCreate()
  2. onStart()
  3. onResume()

after that when you press Home Button then following method call

  1. onPause()
  2. onStop()

Note: onDestroy() method not call after press Home Button.

following code for demo purpose. first run your code in Emulator or Device and after that click HOME Button to check result in your console.

package com.example.checkdataversion;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {
    private static final String TAG = "main";

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    Log.i(TAG, "oncreate");
    setContentView(R.layout.fragment_main);
}

@Override
protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
    Log.i(TAG, "onstart");
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    Log.i(TAG, "onresume");

}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    Log.i(TAG, "onpause");
}

@Override
protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
    Log.i(TAG, "onstop");
}

@Override
protected void onDestroy() {

    super.onDestroy();
    Log.i(TAG, "ondestroy");
}

}

like image 92
vikseln Avatar answered Oct 19 '22 20:10

vikseln


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

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).

like image 27
CommonsWare Avatar answered Oct 19 '22 21:10

CommonsWare


Easy, you just have to override onAttachedToWindow()

@Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);           
}

And then easily catch home button pressed

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

    if(keyCode == KeyEvent.KEYCODE_HOME)
    {

       //do some stuff 

    }
});

Take from http://nisha113a5.blogspot.fr/

Hope this help.

like image 1
Adrien Cerdan Avatar answered Oct 19 '22 21:10

Adrien Cerdan