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.
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
mRunningthat you will set totrueupon launch. Iftrue, then on theonStart()method you start an intent to launch your secondActivity; if false, then go on withsetContentView().If you have several activities to go back to, then a feasible approach is to save the current activity in
SharedPreferencesand 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.