Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
136 views
in Technique[技术] by (71.8m points)

android - Top Activity is removed whenever started from play store and put back to background

My App has different activities. The first Activity shown is the SplashActivity, which will instantly either open the AuthActivity or if authorized the MainActivty.

From the MainActivity it is possible to navigate to further activities, i'll now just reference as OtherActivity.

My Problem is: Whenever i start the app from the Play Store Installation dialog that says "open app", then log in, navigate to OtherActivity, put the app in background and click on the launcher icon of my app, the MainActivity is shown.

However, after killing the app and opening it via the launcher icon, navigating to OtherActivity, putting the app in background, opening it again via the launcher, the OtherActivity is shown.

The Question: How can this be fixed, so that the OtherActivity always stays on top?

So far i could find out, that the difference between launching from playstore and launching from launcher is the category: android.intent.category.LAUNCHER

simulate Play Store: adb shell am start -a android.intent.action.MAIN -n com.example/com.example.SplashActivity

simulate Launcher: adb shell am start -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -n com.example/com.example.SplashActivity

The manifest looks like this:

<activity
    android:name=".SplashActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<activity
    android:name=".MainActivity"
    android:launchMode="singleTask">

    <intent-filter>
        <action android:name="android.intent.action.VIEW" />

        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />

        <data
            android:host="*"
            android:scheme="com.example" />
    </intent-filter>

    <intent-filter>
        <action android:name="android.intent.action.VIEW" />

        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />

        <data
            android:host="*"
            android:scheme="com.example" />
    </intent-filter>

</activity>

Edit: I think this is related to this issue

Edit: The OtherActivity is registered in the manifest and started as follows:

<activity
    android:name=".OtherActivity"
    android:label=""
    android:theme="@style/AppTheme"/>
startActivity(
    Intent().apply {
        addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        setClassName(context, "com.example.OtherActivity")
    }
)
question from:https://stackoverflow.com/questions/65886750/top-activity-is-removed-whenever-started-from-play-store-and-put-back-to-backgro

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

When you pass these flags FLAG_ACTIVITY_CLEAR_TASK|FLAG_ACTIVITY_NEW_TASK your activity will be root activity.

I think what happens is that two intents are mixed up

  1. from Google store
  2. Launch icon are opening your app,

So you could add in your MainActivity and/or SplashActivity oncreate() method to prevent this behavior,

 if (!isTaskRoot()) {
            if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && Intent.ACTION_MAIN.equals(intent.getAction())) {
                finish();
                return;
            }
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...