【发布时间】:2018-08-22 10:07:12
【问题描述】:
从我的 MainActivity(启动器活动)中,我按下一个按钮来启动我的 GameActivity。我有它,所以当我在游戏活动中点击后退按钮时,我不会返回我的 MainActivity 而是返回我的主屏幕。现在,当我恢复我的应用程序时,它会转到 MainActivity 而不是返回到 GameActivity 尽管显示。
目标
主活动 -> 游戏活动 -> 主页 -> 游戏活动
当前结果
主活动 -> 游戏活动 -> 主页 -> 主页活动
几件事..
- 当我使用主页和菜单按钮进行导航时,它可以完美运行。
- 我的清单中没有启动模式
让我们看看我做了什么!
主活动按钮点击
Button startButton = findViewById(R.id.buttonStart);
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, GameActivity.class);
//stops back button to return to add player screen
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
清单简化
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainPage.MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar"
android:screenOrientation="portrait"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".GamePage.GameActivity"
android:label="@string/title_activity_game"
android:theme="@style/AppTheme.NoActionBar"
android:screenOrientation="portrait" />
</application>
【问题讨论】:
-
如何恢复您的应用程序? (步骤)
-
我单击菜单按钮,它会出现,然后我用手指点击它。除此之外我一无所有。我认为它应该只是去正确的活动,因为其他堆栈被擦除了。
-
这些是停止返回上一个活动的解决方案,它们不能解决问题或用我的方法恢复应用程序。这些答案也已在下面的 cmets 中提出,我尝试了您链接中评论的各种不同内容,结果没有变化。
-
我认为您向下滚动链接的速度太快了,可能错过了 Asok 关于“android:launchMode”和 android 文档(“singleTask”或“singleInstance”)的回答,但是,无论如何,很高兴它适用于你。 :)
标签: android android-intent android-activity android-lifecycle back-stack