0

I am trying to understand some behavior of the android apps, now that I faced the following issue :

After device restart my app launches by default, but with one issue : If I put it in the background, and I try to resume it from HOME launcher, it restarts from splash

Weird is that this happens ONLY & ONLY after the device it was installed on, is restarted ! To break this behavior I have to kill the app and launch it again. From that point it works as expected -> If in background, using launcher icon restore the app state

Looking a bit in the logs from lifecycles of the activities in cause, I noticed that the flow is pretty much the same ! With the difference that after onResume of last active activity, the SplashScreen gets launched (this being LAUNCHER activity)

Now, I've seen that this might be already an issue reported to Google, even multiple times, but .. If I cannot fix or workaround it, I am at least trying to understand the flow of what happens on these cases.

Edit: Seems to be related to FLAG_ACTIVITY_NEW_TASK set in the Receiver, but.. then again I cannot start the app after boot is completed without this flag! Feels like a cycle I can't get out of..

Ionut J. Bejan
  • 734
  • 11
  • 28
  • Please provide the code that starts your activity (the boot receiver) and the AndroidManifest.xml file – deluxe1 Oct 29 '18 at 13:33
  • `BootReceiver` is a simple Intent with `FLAG_ACTIVITY_NEW_TASK` set ! About the Manifest, what you'd want to check on it (quite big to paste it all in the question) – Ionut J. Bejan Oct 29 '18 at 13:36
  • I want to see the start modes of the app. It is probably a problem with the flags you add to the intents – deluxe1 Oct 29 '18 at 13:47
  • There is not `launchMode` set on activities so as I've read it is set to `standard` by default – Ionut J. Bejan Oct 29 '18 at 13:51
  • Try removing the FLAG_ACTIVITY_NEW_TASK from your BootReceiver. Since the problem only occurs when the app is started from the BootReceiver, it is likely that this causes the problem. – deluxe1 Oct 29 '18 at 14:01
  • No it won't work ! It will crash because I am trying to launch an activity from outside of context. It needs that flag. Also tried with different flags (same raising a crash because of the above need). As I understood, I tried using also the FLAG_ACTIVITY_SINGLE_TOP which seems suitable for this need, but won't work. – Ionut J. Bejan Oct 29 '18 at 14:04
  • I understand. The thing is, when the launcher icon is clicked, Android starts the activity marked as LAUNCHER in your Manifest file with the FLAG_ACTIVITY_NEW_TASK flag, causing your app to be restarted (the task you've started from the BootReceiver is killed and a new task is started). Try combining the FLAG_ACTIVITY_NEW_TASK flag with the FLAG_ACTIVITY_REORDER_TO_FRONT when starting the activity from the BootReceiver. I am not sure, but it may just do the trick. – deluxe1 Oct 29 '18 at 14:14
  • Well, I will give it a try, even tho I tried using NEW_TASK and SINGLE_TOP.. Also, I was thinking to remove the flag after launching the activity but probably won't have any effect – Ionut J. Bejan Oct 29 '18 at 14:15
  • It won't have any effect, the flag matters when starting the activity. Add both flags and give it a try. – deluxe1 Oct 29 '18 at 14:19
  • no, it doesn't work.. Same behavior as before ! – Ionut J. Bejan Oct 29 '18 at 14:25
  • You may find some clues in the launchMode documentation in https://developer.android.com/guide/topics/manifest/activity-element. It would appear your SplashScreen activity creates a new task when launched from the Home shortcut although there already is one in the background. You could check if that's the case in the recents screen (if you have two tasks/entries for your app). You could use a different launch activity with launchMode="singleTask" to prevent this behavior and use a view or fragment to display the splash screen. – Crispert Oct 29 '18 at 15:08
  • @Crispert read my Edit please, yes it is related to FLAG_ACTIVITY_NEW_TASK. My guess is that using that in the Receiver, system won't see it as same instance of something similar therefore it launches app again.. Weird thing is that `onResume` from last seen activity (before put in background) is called then it goes to `SplashScreen` – Ionut J. Bejan Oct 29 '18 at 15:21

0 Answers0