1

I've done a fair bit of searching for my issue, but can't find any solutions to my exact problem.

I have an app that has 3 activities in succession. The main activity (on launch), the second activity (where most of the meat and guts live), and then a final activity that is more or less a summary page of what happens in the 2nd activity.

I run into this issue semi-sporadically where if I keep the 2nd activity running in the background (turn the screen of my phone off, or just hit home) and then hit the launcher icon to go back to the app, it creates a new instance instead of returning to my 2nd activity that is running in the background. I can't find any rhyme or reason as to why it happens sometimes and not others (usually it doesn't). Most of the time it takes me right back to my 2nd activity and all is good in the world, but on occasion, it fires up another instance.

Is there anyway to check if an instance is running and ALWAYS return to that instance if there is?

Christopher Johnson
  • 2,629
  • 7
  • 39
  • 70
  • 1
    When you say "multiple instances of android app", are we talking about activities? There can only ever be one instance of your app. To answer your question, you first need to understand the activity life cycle and the flags used with intents to control how activities are managed on the navigation stack. You also should understand that once an activity goes to the background (after onPause()), the next time that activity comes to the foreground, it might be a new instance of the activity and onCreate() is called. – Simon Mar 04 '14 at 19:20
  • BTW, I should add that you put your activity in the background and your **entire app might be killed** and started afresh when you tap the icon again. It's up to you what happens then. – Simon Mar 04 '14 at 19:24
  • I guess then they are new instances of the activities. The 2nd activity that I mentioned above plays music. So I know that it never dies because even when my phone screen is off, it's playing music. As I said before, most of the time when I tap my app icon after waking my phone up or coming back from another app, it takes me to the 2nd activiity with music playing. But on occasion, it takes me back to the first activity and if I then go to the 2nd activity, it's a new instance of it. I just want to avoid that happening and have it always take me to the 2nd activity when tapping the icon. – Christopher Johnson Mar 04 '14 at 20:15

1 Answers1

0

When you hit Home or the screen turns off, the app automatically goes through the onStop() phase. Depending upon the available memory, the app might become a candidate to be killed by the Android OS. There really is no way to prevent the OS from killing an app that is no longer in the foreground - this is an Android OS design. This should most likely not happen if you do not have a lot of applications open.

If the first instance of the app is still around and you launch the app, it will automatically retrieve this instance. Android is designed to never create a second instance of the same app while the first one is still running. Hence there is no way to check is an instance is running. You could use logging inside onDestroy() to see when the app terminates. But again, Android does not always call onDestroy() and might just simply terminate the app.

ucsunil
  • 7,378
  • 1
  • 27
  • 32
  • You might also not get onStop() in some scenarios. In general, onPause() is the one to use, along with onResume(). These two are guaranteed unless there is some catastrophe like a sudden power loss or a system crash somewhere. – Simon Mar 04 '14 at 19:32
  • @Simon well as I said in my comment in response to your comment, the 2nd activity is still ongoing as it's playing music so I don't think it's ever hitting onStop()... – Christopher Johnson Mar 04 '14 at 20:26
  • 1
    Are you using a service? In this case, a service can continue to run. Services are not the same as activities. – ucsunil Mar 04 '14 at 20:36
  • doh! ya the music player is in a service and is called statically from the 2nd activity. Is there a way to prevent that activity from ever dying? – Christopher Johnson Mar 04 '14 at 20:40
  • Unfortunately no... the very requirement of a service is to keep running even after if it's host activity is no longer around. – ucsunil Mar 04 '14 at 20:48
  • How much better the question would have been had you provided all of the relevant information! See this, http://stackoverflow.com/questions/1450019/android-restore-last-viewed-activity – Simon Mar 04 '14 at 21:51
  • @Simon that looks like what I need. Put that in an answer and I'll mark it. Thanks. – Christopher Johnson Mar 04 '14 at 21:59
  • I would just be copying someone else. Better to upvote the existing answer which helped you. Good luck. – Simon Mar 04 '14 at 22:00