Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Play and Launcher launched separated activities

Tags:

android

Let's say I have a simple app, with a SplashScreenActivity and a MainActivity.
Below is part of my AndroidManifest.xml:

    <activity
        android:name=".front.activities.splashscreen.SplashScreenActivity"
        android:launchMode="standard"
        android:screenOrientation="portrait"
        android:theme="@style/SplashTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".front.activities.main.MainActivity"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:label="@string/Main_Title"
        android:launchMode="singleTop"
        android:screenOrientation="portrait"/>

And my SplashScreenActivity opens MainActivity in onCreate().

The Problem

If the app is launched from Google Play instead of launcher, if I pressed home and click the app icon in the launcher, one more SplashScreenActivity is launched again and therefore one more MainActivity on the backstack.

Steps to reproduce

  1. Kill the app if it is opened.
  2. Open the app from Google Play.
  3. Press Home button
  4. Open the app from launcher. You will know notice that SplashScreenActivity has been launched again (Or by looking at logs)
  5. Repeat step 3-4. Each time you repeat, one more SplashScreenActivity and MainActivity is launched.

After several trials, if we press back button, we will notice that there are multiple MainActivity in the back stack.

More information

  1. If the app is not started from Google Play, but from launcher at the very first time (step 2), this cannot be reproduced.
  2. Not only Google play, but any other app that sends an intent to start the app can reproduce this.
  3. If I first launch from launcher, and then from Google Play, 2 SplashScreenActivity will be launched. But if I press app icon from launcher again, it will not create a 3rd SplashScreenActivity. It will bring the first launched MainActivity to the top.

What have I tried

  1. Make SplashScreenActivity to android:launchMode="singleTask". Does not help.
  2. Make MainActivity to android:launchMode="singleTask". This will prevent multiple MainActivity from being created, but does not solve the problem of starting SplashScreenActivity multiple times. You may also assume MainActivity should not be set to singleTask.

What I expected

By clicking on the app icon in the launcher, Android should be able to find out that in my task manager, the app is already launched and will simply bring it to the top.

How can I fix this?

like image 302
Sira Lam Avatar asked Oct 03 '18 07:10

Sira Lam


People also ask

How many launchers activities can be present in an Android app?

Yes, You can have more than one launcher activity in your application. This will not create any kind of compile-time or run time error.

How do I remove a release from Google Play console?

Open Play Console. Select an app. Go to Release > Setup > Advanced settings. On the App Availability tab, select Unpublish.

What are the steps to setup managed Google Play?

Sign in with a Google Account that isn't already associated with a managed Google Play Accounts enterprise. Click Get started. Enter a name for your managed Google Play Accounts enterprise. Click Confirm.


1 Answers

Some launchers have this bug: when app is started from home screen, new instance of initial activity is created instead of resuming the app. It can be fixed by adding

if (!isTaskRoot()) {
    finish();
    return;
}

to onCreate() of the initial activity. See also:

Resume last activity when launcher icon is clicked,

Resume the Top Activity instead of starting the Launcher Activity

like image 161
atarasenko Avatar answered Oct 20 '22 07:10

atarasenko