【问题标题】:Open recent activity on app icon press , after pressing the home key of device按下设备的主页键后,按应用图标打开最近的活动
【发布时间】:2017-06-19 11:53:31
【问题描述】:

我的 Launcher 类名称是“SplashScreen”。我有两个活动“MainActivity”和“MenuList”。当用户打开应用程序时,会启动 SplashScreen Activity。启动屏幕活动验证并启动 MainActivity。用户通过单击 MainActivity 屏幕中的按钮打开 MenuList Activity,然后单击 MenuList Activity 中的主页按钮。当用户打开应用程序时,它应该直接打开 MenuList Activity。我已按照以下链接中的说明进行了更改。当用户在 MainActivity 中按下主页按钮时,它工作正常。当用户在 MenuList Activity 中按下主页按钮时,它不起作用。请帮帮我。

https://stackoverflow.com/a/20815679/1517280

屏幕流: SplashScreen -> MainActivity -> 菜单列表

清单代码:

 <activity
            android:name=".SplashScreenActivity"
            android:clearTaskOnLaunch="true"
            android:launchMode="singleTask"
            android:screenOrientation="portrait" >
            <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>
        </activity>
<activity
            android:name=".MainActivity"
            android:windowSoftInputMode="adjustResize|stateHidden"
            android:screenOrientation="portrait" />


<activity
            android:name=".MenuList"
            android:windowSoftInputMode="adjustResize|stateHidden"
            android:screenOrientation="portrait" />

【问题讨论】:

  • SplashScreen 中删除android:clearTaskOnLaunchandroid:launchMode。还要从链接的答案中删除您添加的时髦代码。你不需要这些。您想要的行为是标准的 Android 行为。

标签: android android-intent savestate


【解决方案1】:

从 SplashActivity 的元素中删除 android:clearTaskOnLaunch="true"

事实上,这对您的 MainActivity 也不起作用 - 因为设置了此标志,所以每次您返回应用程序时,都会清除任务的堆栈并启动 SplashActivity。

【讨论】:

  • 我在 SplashActivity 中添加了以下代码。它适用于 MainActivity protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) { // Activity 被带到前面但没有创建, // 因此完成这将使我们进入最后查看的活动 finish();返回; } // 常规活动创建代码... }
  • 谢谢。它现在正在工作。我已经从 SplashActivity 中删除了 android:clearTaskOnLaunch="true"
  • @SrikanthReddyRamidi,然后考虑接受答案。
【解决方案2】:

我得到了我想要的结果。我做了以下修改:

清单文件:

<activity
            android:name=".SplashScreenActivity"
            android:launchMode="singleTask"
            android:screenOrientation="portrait" >
            <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>
        </activity>
<activity
            android:name=".MainActivity"
            android:windowSoftInputMode="adjustResize|stateHidden"
            android:screenOrientation="portrait" />


<activity
            android:name=".MenuList"
            android:windowSoftInputMode="adjustResize|stateHidden"
            android:screenOrientation="portrait" />

我在 SplashActivity 中添加了以下代码:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            // Activity was brought to front and not created,
            // Thus finishing this will get us to the last viewed activity
            finish();
            return;
        }
//...rest of the code
}

【讨论】:

    猜你喜欢
    • 2014-01-15
    • 1970-01-01
    • 2011-11-28
    • 1970-01-01
    • 2014-12-24
    • 2017-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多