简体   繁体   中英

Android opening app from Playstore vs Launcher behavior

I observed that if I open some apps installed in my phone via Google Play's 'Open Now' button the app will open as normal, if I navigate to another Activity inside the app, then pressed home, then go to phone's launcher, open the app through there. It will start a new task -- I believe -- because the App's launcher activity has resurfaced. If I press back, I will then be in the second Activity -- which is the last Activity in the foreground when I launched the app from Google Play. Is there a way to avoid this? I'm expecting to see the second activity in this scenario. I want to have the same task whether my app is launched from the Launcher or Google Play store.

I found out that a lot of apps in the Playstore has this behavior. One good example is the Zomato app and for reference my app is Shake Eat Off

I've been experimenting the android:launchMode in the Manifest with no luck.

I tried setting singleTask to the root activity and `singleTop to the second Activity

I also tried adding android:alwaysRetainTaskState="true" to the root Activity.

I also tried setting singleTask to the root Activity and singleInstance to the second Activity

So to explain more the chain:

Google Play App Page -> Open Now -> Root Activity -> Second Activity(now foreground). Press home, Click App Icon. At this point, The root Activity is now again showing (But I'm expecting to see the second Activity because it's the last activity on the foreground). Press back -> The Second Activity will resurface.

It happens because launcher intent is started again. I see someone post a short solution in a discussion community. And I came up with a fully solution with the original solution. In your splash screen you place these codes in onCreate()

if (!isTaskRoot()) {
    finish();
} else {
    // Place your current onCreate() codes
    // We put code here because finish() doesn't kill the activity immediately while OS takes a while to kill it and we don't need codes to be execute
}

These code help you not skip new task and bring up all old created tasks

In your splash screen if there are some codes in onResume() Place them in the following condition

if (isTaskRoot()) {
    // Place your current onResume() codes
}

This help your onResume() codes do only when the task the old root task. Use this because finish() function in onCreate() doesn't kill the new Splash screen immediately while OS takes a while to kill it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM