2

I came to do the eternal question, which so far have not found a solution, I searched on the internet the same problem but found a final solution to this problem.

when I have 2 activities open and I pull the 'Home Button' and then press the shortcut for my application, it shows me again the first activity (the launcher activity), and then to return to the activity that was displayed, I have to press the back button.

what is the solution to this problem?

I want to press the shortcut of my application (after having left my application by pressing the Home Button) show me the last activity was displayed, instead it shows me the first activity (activity launcher).

Thanks in advance.

seba123neo
  • 4,688
  • 10
  • 35
  • 53
  • possible duplicate of [How to make an android app return to the last open activity when relaunched?](http://stackoverflow.com/questions/2441203/how-to-make-an-android-app-return-to-the-last-open-activity-when-relaunched) – Aleadam Jun 16 '11 at 04:36

4 Answers4

0

Depending on whether or not your main activity is ever launched by another activity, or only the application icon, you can use a much simpler solution. If your main activity is only launched by the application icon, you can use isTaskRoot() to check if your main activity is being launched as a fresh start to the application, or if the user is returning and the main activity is being laid on top of other activities that you wish to display instead.

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

    if (! isTaskRoot()) {
        finish();
    } else {
        ...
    }
}
ErlVolton
  • 6,714
  • 2
  • 15
  • 26
0

You can use startActivityForResult replace for startActivity when you want to open another Activity.

0

Your application is still running in the background when you press the home button. finish() the activity when you press the Home button if you want to go back.

ahodder
  • 11,353
  • 14
  • 71
  • 114
  • I do not understand, I want to press the shortcut of my application (after having left my application by pressing the Home Button) show me the last activity was displayed, instead it shows me the first activity (activity launcher). – seba123neo Jun 15 '11 at 21:47
  • 1
    Oh, I'm sorry I misinterpretted. You application then may have then been killed for memory. What you could do is created a sharedpreference for the application, and every time the activity changes update the preference. Then when ever the application restarts, check the preference and hop straight the the desired activity. – ahodder Jun 15 '11 at 21:53
0

That is the expected behavior. The launcher will launch the Activity with the filter android.intent.action.MAIN.

There are ways to work around it, though. A very simple one is to have a boolean flag mRunning that you will set to true upon launch. If true, then on the onStart() method you start an intent to launch your second Activity; if false, then go on with setContentView().

If you have several activities to go back to, then a feasible approach is to save the current activity in SharedPreferences and launch it the same way.

Alternatively, your main Activity may be just an entry Activity whose only job is to start the last Activity used.

EDIT: I found this duplicate question: How to make an android app return to the last open activity when relaunched? that has a much better ansewr than mine.

Community
  • 1
  • 1
Aleadam
  • 40,203
  • 9
  • 86
  • 108
  • thanks "Aleadam", the link that you passed me, is the solution to the problem. I have already implemented and works perfect. – seba123neo Jun 16 '11 at 19:22