12

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?

Arnab
  • 181
  • 1
  • 2
  • 4

8 Answers8

18

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.

Catalin Morosan
  • 7,897
  • 11
  • 53
  • 73
  • 1
    A better workaround can be found at http://code.google.com/p/android/issues/detail?id=2373#c21 – David Wasser May 22 '12 at 16:16
  • Reading other people's comments, it seems it is not certain that that approach works on all versions. For the moment, I'm sticking with this because I have been using it for almost 2 years and it is working great. – Catalin Morosan May 25 '12 at 07:49
  • Seems to me that your solution has a hole. The boolean `MyApplication.startedApp` is set when the application is started and is never cleared. This means that if the user starts your app and then clicks the BACK button, there will be no Activity in the activity stack but `MyApplication.startedApp` will be `true`. If the user then starts the app again from the home screen this will launch your DummyActivity which will then immediately call `finish()`. Your code assumes that the OS will kill your application's process immediately once the user leaves the application, which I don't think is good – David Wasser May 25 '12 at 08:11
  • You're right. I've added the missing part which I was already using in my own code. Intercept onBackPressed and move startedApp to false. – Catalin Morosan May 28 '12 at 14:05
  • 3
    Having tackled this for months now, I'm confident in a better, 'framework correct' solution which does not require maintaining your own app/stack state. It is a slight enhancement of the #21 fix referenced above and is available here: https://code.google.com/p/android/issues/detail?id=2373#c40 – Mike Repass Jun 27 '13 at 18:40
  • @MikeRepass ur suggestion works fine but in my case i am having SplashActivity as launcher activity and when i use [THIS](https://code.google.com/p/android/issues/detail?id=2373#c40) in my app the The SplashActivity is finishing after some time The HomeActivity which will launch from SplashActivity is Launching Any Help – W I Z A R D Apr 26 '14 at 07:03
2

Does the platform maintain any history entry when pressed home key?

Yes. Activities when launched/finished are pushed/pop'd onto/from a History Stack.

How do we take the user to the last launch activity on relaunching the application?

AFAICT, that should be the default behavior. When you relaunch your application you are taken to the last Activity on the stack unless you alter the stack.

Read this for details: Application Fundamentals - Activities and Tasks

Samuh
  • 36,316
  • 26
  • 109
  • 116
1

This is a bug with Android OS.

Please perform following steps.

1)Delete your application and reinstall it.

2) After installing the application, it will display two option "Done" & "Open" click on Done button.

3) Now open the application from the application menu. Your problem will be solved.

When ever the HOME button is pressed, the application will start from the state where it left.

Gautam Mandsorwale
  • 1,580
  • 1
  • 18
  • 27
0

when you press the home button the application will be disappear , when relaunch the application the foreground activity will be resumed ,

the reason is its the behavior of android operating system

John
  • 1,407
  • 7
  • 26
  • 51
0

I can also recommend reading Activity and Task Design Guidelines. These are the user interface guidelines on how to behave when the Home button pressed, when the Back button is pressed, and so on.

It's not very technical, but it illustrates very well how your app should behave and what the OS expects.

pableu
  • 3,200
  • 4
  • 23
  • 21
0

I had a similar problem. It wasn't launching the last open activity but the Launcher Activity. But I had also put

android:launchMode="singleTask"

in my Launcher Activity in the manifest. Removing that fixed my issue. If you have also put the same thing then try removing it, it might fix for you too.

Renegade
  • 642
  • 1
  • 10
  • 19
0

You cannot handle Home Button... Android development page says home button newer handling.. it is easy to understand..Thank you

metin
  • 33
  • 1
  • 6
0

The launch mode of your activity can affect the behaviour when it is relaunchd after pressing home. If the first activity was launched as singletask or singleinstance then , its always the first activity that will be resumed.

If activities are launched using default launch mode, then when the application is restarted, the previous activity will be resumed

png
  • 4,368
  • 7
  • 69
  • 118