Application launcher activity reopens when opening app from background

问题: I have an app on the Google Play Store. I've found an interesting bug here - when I'm sending my app to background (by pressing home) and then clicking on the app icon agai...

问题:

I have an app on the Google Play Store. I've found an interesting bug here - when I'm sending my app to background (by pressing home) and then clicking on the app icon again, it opens the main screen.

I read some article (https://medium.com/@elye.project/three-important-yet-unbeknown-android-app-launcher-behaviors-part-2-139a4d88157) about it, tried all application states, but the behavior remains the same. However, If I will open my ADB app (or from Studio) all works just fine.

How to solve this? My manifest file looks like following:

<uses-feature
    android:name="android.hardware.touchscreen.multitouch"
    android:required="false" />

<uses-feature
    android:name="android.hardware..accelerometer"
    android:required="true" />

<uses-feature
    android:name="android.hardware.telephony"
    android:required="false" />

<application
    android:name=".MyApplication"
    android:launchMode="singleTop">

    <activity
        android:name=".ui.main.MainActivity"
        android:configChanges="orientation|screenSize"
        android:screenOrientation="portrait"
        android:launchMode="singleTop">

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

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.LAUNCHER" />
        </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="signin"
                android:scheme="appprefix" />
        </intent-filter>
    </activity>

    <activity
        android:name=".ui.view.Acitivity2"
        android:configChanges="orientation|screenSize"
        android:screenOrientation="portrait"
        android:launchMode="singleTop"/>

    <activity
        android:name=".ui.view.Acitivity3"
        android:configChanges="orientation|screenSize"
        android:screenOrientation="portrait"
        android:launchMode="singleTop"/>

回答1:

A possible workaround for market launches. The market launches the main activity on top of other activities. We never want this to happen. Instead, we should check if we are the root and if not, we finish the current. Add following code on your launcher activity onCreate()

 if (!isTaskRoot()) {
        final Intent intent = getIntent();
        if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && 
              Intent.ACTION_MAIN.equals(intent.getAction())) {
            finish();
            return;
        }
    }
  • 发表于 2018-12-31 19:30
  • 阅读 ( 165 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除