Search code examples
androidactivity-stack

How to resume activity when application relaunched from android launcher?


I have an application that implements two activities. Activity A is for selecting some files on device and activity B is to show additional info while these files are processing. Both of them have singleInstance as launch mode.

On application's start activity A runs. Then this activity starts second activity B, that creates a notification. If I tap this notification or open running app from recents, it works fine and shows running activity B. But if I'd started application before and activity B is already running, launching it again from app menu causes showing activity A when old activity B is already running and accesible from notification bar.

So, what should I do to make application run only single activity at the same time and show second activity when called from launcher (if second activity once started and isn't finished)?


Solution

  • Your launcher intent should be mapped to a dummy activity, with no view (or may be a splash who knows !). In onCreate of that dummy activity you check if a state was saved previously and based on the state switch to the desired activity.

    And how to set the state ? well lets try something simple. lets say when Activity A resumes we put "A" to some key in SharedPreference. if Activity B resumes we put "B" to the same key in preferences. Now your application goes out when Activity B was visible, the state saved would be "B", you launch from home screen, the dummy activity finds "B" in the state and launch intent to go to B else go to A.