1

I am trying to fix a bug in my code, and am hoping someone can point me in the right direction. If my app goes into the background, and gets resumed by the user just going into "apps" and selecting my app, everything works fine.

However, if they put it into the background and resume it by holding down the home button, and selecting it from the recent apps list, I get a bug. I was under the impression these two actions should do the exact same thing?

Does anyone know what the difference is between resuming an app from the normal list of apps, vs resuming it by holding the home button down and then selecting the it from that list?

Alex K
  • 8,269
  • 9
  • 39
  • 57
akhalsa
  • 2,303
  • 3
  • 25
  • 43
  • Please post the error you are getting. – wtsang02 Apr 26 '13 at 16:36
  • You are correct. These 2 things do exactly the same thing. You are seeing something else. Probably you are seeing this nasty bug: http://stackoverflow.com/a/16447508/769265 – David Wasser Jan 05 '15 at 14:28
  • @akhalsa, long-pressing the home button on KitKat 4.4.2 (API 19) opens Google Now on my device. I am stuck in a similar situation as you were, can you please clarify what you mean by, "resuming an app from the normal list of apps"? Are you referring the navigation `Settings -> Apps`? Thank you! – Narayana J Jun 24 '16 at 16:19

1 Answers1

5

The exact answer depends a bit on the implementation of your Home Screen or Launcher App. However, from what I experienced so far I'm pretty sure that ...

  1. ... apps, that are started from your menu or home screen are usually started by an Intent. To be more precise: the active launcher shows all activities in its menu that have an action android.intent.action.MAIN" and category android.intent.category.LAUNCHER and if you select an app, it creates an Intent and by this start the app:

    Intent intent = new Intent(Launcher.context, SelectedActivity.class);
    intent .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
    startActivity(intent);
    

    This creates most likely a new instance of your started app activity, apart from some flags that might avoid this (android:launchMode="singleTop")

  2. ... looking at recent tasks this works differently as far as I could see: the task list relies most likely on a list that is created by getRecentTasks() and brings the selected app to front. This can be done by using moveTaskToFront(). Only if the app/activity has been finished it is newly created (try killing the app, and you'll see it is recreated).

Conclusion: as you can see the recent task list works rather like a (go) back to the app causing potentially an onResume() whereas starting the app from the menu will cause an onCreate().

Note: one application package might contain more that one app. The Contacts and Phone app are in many cases just two different activities in one supplied application package (i.e. APK file).

Hope this helps to understand the different behavior ... Cheers!

Trinimon
  • 13,839
  • 9
  • 44
  • 60
  • 1
    This answer is wrong. If your app is running and in the background and you return to the HOME screen and then launch the app again by pressing on it's launcher icon, it will simply move the existing task from the background to the foreground, without calling `onCreate()`. This is the exact same behaviour as if you had returned to the app from the list of recent tasks. It is the same thing. There is no difference. – David Wasser Jan 05 '15 at 14:25
  • @David: unfortunately there is a difference - I was facing the same issue as _akhalsa_. What is your explanation for this? Don't just say it's wrong - it has happen - really ;) – Trinimon Jan 08 '15 at 07:36
  • It is possible that what you were seeing is this nasty Android bug: http://stackoverflow.com/a/16447508/769265 This bug shows up if you launch your app from an IDE (Eclipse, IntelliJ) or directly from the Android installer (click the "open" button after installation). If the app was started like this, and you press HOME and then launch the app again from the HOME screen, Android incorrectly creates another instance of your root Activity. This is an Android bug. It isn't supposed to work like this, and in fact, it doesn't work like this if you start your app initially from the HOME screen. – David Wasser Jan 08 '15 at 10:04
  • 1
    However, after spending years working with this and reading the Android source code, I can assure you that (except for this one known bug), there is no difference. – David Wasser Jan 08 '15 at 10:05