在我的 MainActivity(启动器 Activity )中,我按下一个按钮来启动我的 GameActivity。我有它,所以当我在游戏 Activity 中点击后退按钮时,我不会返回到我的 MainActivity 而是返回到我的主屏幕。现在,当我恢复我的应用程序时,它会转到 MainActivity,而不是返回到 GameActivity,尽管已显示。
目标
Main Activity -> Game Activity -> Home -> Game Activity
当前结果
Main Activity -> Game Activity -> Home -> Home Activity
几件事..
- 当我使用主页和菜单按钮进行导航时,它工作得很好。
- 我的 list 中没有启动模式
让我们看看我做了什么!
主 Activity 按钮点击
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);
}
});
简化 list
<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>
最佳答案
在这种情况下,只需在启动 GameActivity 后立即调用 finish()。
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);
startActivity(intent);
finish();
}
});
编辑:同时更新 GameActivity 的启动模式:
<activity android:name=".GameActivity" android:launchMode="singleInstance">
关于android - 回到主页后恢复 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49268240/