【问题标题】:Application launcher activity reopens when opening app from background从后台打开应用程序时,应用程序启动器活动重新打开
【发布时间】:2018-12-28 15:33:27
【问题描述】:

我在 Google Play 商店中有一个应用程序。我在这里发现了一个有趣的错误 - 当我将我的应用程序发送到后台(通过按主页)然后再次单击应用程序图标时,它会打开主屏幕。

我阅读了一些关于它的文章 (https://medium.com/@elye.project/three-important-yet-unbeknown-android-app-launcher-behaviors-part-2-139a4d88157),尝试了所有应用程序状态,但行为保持不变。但是,如果我打开我的 ADB 应用程序(或从 Studio),一切正常。

如何解决这个问题?我的清单文件如下所示:

<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"/>

【问题讨论】:

  • 您的意思是,如果您在Acitivity2 上并按下主页按钮,那么当您返回应用程序时,MainActivity 会恢复,而不是Acitivity2
  • 是的。当我按下主页按钮然后单击图标时会发生这种情况。而且我发现了一些错误 - 如果在“重新启动”后我单击 onBack,那么我可以看到我以前的活动
  • 奇怪。这只发生在 Play Store 版本的应用程序中?您是否 100% 确定 Play 商店应用代码在调试时与您的完全相同?
  • 是的。这是相同的代码。当我在设备 throm fileexplore 上安装 apk 时,我可以重复这个错误,但抛出 adb - 不是
  • 尝试从应用程序的标签中删除singleTop。如果您不需要,也将其从所有活动中删除。然后再试一次,使用文件资源管理器。

标签: android


【解决方案1】:

市场发布的一种可能的解决方法。市场在其他活动之上启动主要活动。我们绝不希望这种情况发生。相反,我们应该检查我们是否是根,如果不是,我们完成当前的。在您的启动器活动 onCreate() 上添加以下代码

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

【讨论】:

  • 它有效,但部分有效。我可以看到,如何加载我的主要活动(onCreate 调用)以及如何启动其他活动(旧)。这对用户来说有点随意。如何避免这种视觉影响? super.onCreate(savedInstanceState) if (!isTaskRoot) { if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && Intent.ACTION_MAIN == intent.action) { finish() return } } setContentView(R.layout.activity_main
  • 准确地说,Activity 2 正在打开,但 MainActivity 打开到,但是因为我们用你的代码阻止了它,所以它是在引用。但用户可以看到它
  • 我使用的是同一行代码,但没有看到任何视觉效果。也许我的活动非常轻量级,它在没有任何视觉效果的情况下完成。你可以帮助的半透明背景
  • 如果我从 Stuido 跑起来,我看不出有什么不同。但是当我安装它时,它会抛出手动 apk 安装,我可以看到它(我不知道为什么)
  • 我只给了你解决方法,这是你可以检查的错误issuetracker.google.com/issues/64108432
猜你喜欢
  • 2021-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-08
  • 1970-01-01
相关资源
最近更新 更多