Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Home key press behaviour

Tags:

android

While developing a sample android application i have constructed two activities 1)Activity 1 2)Activity 2

Now Activity 2 is the foreground activity whereas Activity 1 is the background one. Now user presses Home key. The application(i.e. both the activities) dissappear. Now is we relaunch the application we see Activity 1 as the foreground activity. My question is:

1)Does the platform maintain any history entry when pressed home key? 2)How do we take the user to the last launch activity on relaunching the application?

like image 956
Arnab Avatar asked Jun 15 '10 03:06

Arnab


People also ask

How can I tell if Android studio is pressing my home button?

Then the following event WILL get fired for home button presses: @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode == KeyEvent. KEYCODE_HOME) { //The Code Want to Perform. } });

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

Pressing the home button from an application invokes onPause() and onStop().


1 Answers

I've struggled with this odd behavior for more than a month but I finally found out the explanation by trial and error.

This behavior happens when you start your application from Eclipse, from command line or if you install an application and press on the Open button(instead of the Done button) to start the application right after you installed it.

If in one of those cases, you start your apllication, go to Activity1 and then to Activity 2, press HOME button and then press the application icon, it will open a new instance of Activity1. Don't take my word for it. Just press BACK and see that it gets you to your Activity2 that you left when you pressed HOME.

It seems that the launcher activity is not put on the activity stack if the application is started in one of those ways mentioned above so that's why it creates a new instance of the launcher activity on top of the current activities in the application's stack. This looks to me like a bug.

So, the workaround would be to exit the application, the first time it was started from Eclipse or command line or Open button etc., by pressing the BACK button as many times as needed, and then enter the application again. From then on, the behavior will be as expected.

EDIT: A better workaround would be this: Create a DummyActivity and set it up as the main entry point into the application. Also, add the flag android:noHistory="true". The DummyActivity is simple and would look like this:

public class DummyActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (!MyApplication.startedApp) {
        Intent intent = new Intent(this, HomeActivity.class);
        startActivity(intent);
    }

    finish();
} }

MyApplication is a class that extends android.app.Application and is defined inside AndroidManifest.xml. In HomeActivity.class you set inside the onCreate() method the boolean field startedApp to true. If the user presses BACK from the screen, you need to move the value for startedApp to false.

public class HomeActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    MyApplication.startedApp = true;
}

@Override
public void onBackPressed() {
    MacawApplication.startedApp = false;
    super.onBackPressed();
}

}

So, the first time the app is launched it enters the if block and launches the first real activity in our application. If you navigate through the app, then press HOME and then launch the app again, DummyActivity will get called a second time and it will just call finish() on itself and the app will show the last activity before you pressed HOME.

like image 139
Catalin Morosan Avatar answered Nov 15 '22 18:11

Catalin Morosan