6

I'm working on large project, so there is some logic for saving application state, and then opening correct activity(fragment) when it comes from background. But anyway, I've found that if user navigates through my app and then minimize it - android opens it from background in different ways in following cases:

  1. User taps on app icon (behavior: user see home activity, doesn't matter where he was, when application was minimized)
  2. User select app from android "recent apps" (behavior: user see exactly what he saw, when the application was minimized)

So, what is the difference between launching application from background by this two ways? I always thought, that it is the same mechanism, but, obviously, I was wrong.

Thanks for any answers

OFFmind
  • 597
  • 1
  • 5
  • 13

2 Answers2

7

You should pay atention on the folowing docs Activity and Tasks. In short words: if user start app from recents you will receive onRestart before onStart (without onCreate it means that your app was just "suspended"). You able to save screen state using onSaveInstanceState(). But in general starting from icon and from recents - different application behaviors and you should provide proper code for this ways.

UPD As described below root cause of unexpected behaviour was additional lunchmode attribute.

x90
  • 2,140
  • 15
  • 25
  • Thank you very much! In general maybe, but you know, if specification is written, all should work according it) – OFFmind Sep 12 '13 at 08:35
  • Sorry, but now I've see logs, and onRestart calling in every case (from recents and from tapping app icon), according to the logs from this methods (onCreate, onRestart, onStartm onResume) - both ways working same to each other – OFFmind Sep 12 '13 at 08:45
  • Sorry, but are you sure that you catch case 1 (User taps on app icon (behavior: user see home activity, doesn't matter where he was, when application was minimized) ) if you just minimize your app and then start from icon? Looks like you minimize, remove from recents and than starts form icon. – x90 Sep 12 '13 at 09:12
  • Yes, I'm sure. And i totally can't understand where is that difference in android system, that two different calls to background task causes two different behavior – OFFmind Sep 12 '13 at 09:31
  • I've found the reason. If Activity A is a "singleTask", and you navigate from it to activity B, you will see such behavior Could you update your answer, please, so everyone can easily see it, if face that problem? Thanks. – OFFmind Sep 12 '13 at 10:19
  • Oh, i wanted to ask you about lunch mode modifications but you ahead me. You start your activities with default lunch modes or use manifest attributes or intent flags? – x90 Sep 12 '13 at 10:30
  • In manifest file I've set launchMode to singleTask, and it is the issue. I can't remove it, because it will break other functionality, so for now I'm looking workaround, so the answer for this question is discovered. And it is very important, as many developers(as I before) consider that both ways work the same. It isn't true, anyway) – OFFmind Sep 12 '13 at 10:39
0

From what I experience as an Android user, both are same.

The difference we usually see is how we close the app

  1. Press back button until app close / finish()

    On this state no matter how we open the apps it will go to the main screen

  2. Press Home button

    On this state depend on the app. If the app does not handle any Activity the app will same with the first state. But if the app handle something like when onPause() the Activity then finish() the apps, then whatever you open with app icon or recent apps will provide the same result.

Correct me if I am wrong

Sieryuu
  • 1,510
  • 2
  • 16
  • 41
  • User minimizes app in one way(by tapping home button). But when it launch it again, what he will see depends on the way, how he opens it. That was strange for me, as I thought exactly like you, before faced it – OFFmind Sep 12 '13 at 08:38
  • Yeah, if minimize only with home button. so the answer is only the second state – Sieryuu Sep 12 '13 at 08:42