1

everybody.

I've meet a situation which is very interesting, and I want to share and pursue the theory.

I have two activities: Splash and Main(Main's launchMode is SingleTask) I finish the Splash first then start Main. Then close back key and close app, when I resume the APP by the recent task, it shows Main instead of Splash. finish first then start

Oppositely, if I startActivity Main first then finish, it will be normal: Close APP, return Splash from the recent task

Here is the demo address

I want to know the reason, hope you guys can help!

Thank you

韩建飞
  • 81
  • 5
  • You sure that it happens every time? You have a delayed Handler could it be that you are minimizing/maximizing too fast? – Murat Karagöz Jan 15 '18 at 12:42
  • By default activity state manage by android OS. After splash screen your main activity comes in function and when you press back, you did nothing in code it back to home screen activity of your default launcher. Now in recent task your main activity is manage. When you enter in from recent `onResume()` function will executed, it won't open splash screen. If your app is totally killed even by memory management of android then it will start splash activity. To learn more refer this [link](https://inthecheesefactory.com/blog/understand-android-activity-launchmode/en) – Dee Nix Jan 15 '18 at 12:52
  • @MuratK. yes, please download the demo and check, handler delay just mock splash's animal, it's not related to it. Don't mind it – 韩建飞 Jan 15 '18 at 13:02
  • @DeepakOjha thanks, but the difference is the order of finish and startActivity, I want to know why the different order cause this situation. if startActivity main first then finish Splash, when back to APP from Recent Task after app closed, it will be ok that it will open Splash If finish Splash then startActivity Main, when back to APP from Recent Task after app closed, it will be strange that it will open Main(execute onCreate function) – 韩建飞 Jan 15 '18 at 13:08
  • It's difficult to find a documentation about that. Maybe there is more in [this one about Recent Screen](https://developer.android.com/guide/components/activities/recents.html). As you finish Splash before starting Main, I think something goes wrong and the system recovers the Task where Main was instead of restarting from scratch. – Eselfar Jan 15 '18 at 13:27
  • If you remove `singleTask` it works as there is only one Task and no Activity into it, so the system launches Splash as it's the first Activity of the Task. In any case you're not supposed to finish the activity before using it to start a new one. But it's an interesting question – Eselfar Jan 15 '18 at 13:30
  • @Eselfar yes, you are right. If remove SingleTask, it's ok, too. – 韩建飞 Jan 15 '18 at 13:34
  • @Eselfar Maybe the system goes wrong and the system recovers the Task Main instead of Splash, when finish first then start and Main is singleTask. – 韩建飞 Jan 15 '18 at 13:42
  • @Eselfar AnyWay, thank you for your answering – 韩建飞 Jan 15 '18 at 13:43
  • You are right. When you call `finish` first it totally destroy that actvty from memory and start main activty. You press back and open from recent it is opening main bcoz splash is already destroyed from memory. But when you call `finish' after `startActivity(x);` the code `finish` not working properly bcoz another activity already started. It still hidden somewhere in memory. When you try to open app from recent splash comes to visible. I have learnt one thing here that I should call `finish` before start any single top activity to make sure current activity destroy from memory. Thanks 韩建飞 :) – Dee Nix Jan 15 '18 at 13:44
  • Maybe you'd be interested by this: https://stackoverflow.com/questions/4182761/finish-old-activity-and-start-a-new-one-or-vice-versa – Eselfar Jan 15 '18 at 14:46
  • @DeepakOjha Thank you for your description, but I still suggest that you should call Finish after StartActivity. Cause if its order is reversed, the way of system doing is not stabled and predicable. It's in order to discuss about this situation and know why for us that I hold this question. Hope you not be confused. – 韩建飞 Jan 16 '18 at 01:33

0 Answers0