Android:如何切换到已经创建的活动
我是Android编程新手。 我想做的是切换到另一个已经创建的活动。 假设我启动了活动B并从活动A移动到活动B,然后我按下后退按钮并返回到活动A。 现在我想在倒计时结束后切换到活动BAndroid:如何切换到已经创建的活动,android,android-intent,Android,Android Intent,我是Android编程新手。 我想做的是切换到另一个已经创建的活动。 假设我启动了活动B并从活动A移动到活动B,然后我按下后退按钮并返回到活动A。 现在我想在倒计时结束后切换到活动B ActivityA.java private void startTimer() { ... @Override public void onFinish() { // force the user to move on to Activi
ActivityA.java
private void startTimer() {
...
@Override
public void onFinish() {
// force the user to move on to Activity B
// if the user haven't started Activity B, just start it
if (!mHasActivityBStarted) {
Intent intent =
new Intent(ActivityA.this, ActivityB.class);
startActivity(intent);
} else {
// how can I switch to ActivityB that has been created?
}
}
}.start();
}
我该怎么做?来自文档
将活动标记为重新排序到前端
public static final int FLAG_ACTIVITY_REORDER_TO_FRONT
如果在传递给Context.startActivity()的意图中设置,则此标志将
使已启动的活动位于其任务的最前面
历史堆栈(如果已在运行)
例如,考虑一个由四个活动组成的任务:A、B、C, D.如果D调用startActivity()的目的是解决 活动B的组件,则B将被带到 历史堆栈,其结果顺序为:A、C、D、B。此标志将 如果还指定了标志\活动\清除\顶部,则忽略此选项
在您的情况下,您可以在ActivityA
和ActivityB
之间切换,而无需完成或重新创建它们
把它放在一起
活动a
// Call this method when users press a button on ActivityA to go to ActivityB.
public void goToActivityB(View view) {
Intent intent = new Intent(this, ActivityB.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
// When users press a button from ActivityB, ActivityA will be bring to front and this method will be called by Android.
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// Write your logic code here
}
活动b
// Call this method when users press on a button in ActivityB
public void backToActivityA(View view) {
Intent intent = new Intent(this, ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
// When users press a button from ActivityA, ActivityB will be bring to front and this method will be called by Android.
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// Write your logic code here
}
从文件
将活动标记为重新排序到前端
public static final int FLAG_ACTIVITY_REORDER_TO_FRONT
如果在传递给Context.startActivity()的意图中设置,则此标志将
使已启动的活动位于其任务的最前面
历史堆栈(如果已在运行)
例如,考虑一个由四个活动组成的任务:A、B、C, D.如果D调用startActivity()的目的是解决 活动B的组件,则B将被带到 历史堆栈,其结果顺序为:A、C、D、B。此标志将 如果还指定了标志\活动\清除\顶部,则忽略此选项
在您的情况下,您可以在ActivityA
和ActivityB
之间切换,而无需完成或重新创建它们
把它放在一起
活动a
// Call this method when users press a button on ActivityA to go to ActivityB.
public void goToActivityB(View view) {
Intent intent = new Intent(this, ActivityB.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
// When users press a button from ActivityB, ActivityA will be bring to front and this method will be called by Android.
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// Write your logic code here
}
活动b
// Call this method when users press on a button in ActivityB
public void backToActivityA(View view) {
Intent intent = new Intent(this, ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
// When users press a button from ActivityA, ActivityB will be bring to front and this method will be called by Android.
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// Write your logic code here
}
我建议谷歌选择“安卓活动启动模式”,我建议谷歌选择“安卓活动启动模式”谢谢,我认为它解决了我的问题,但我仍然不明白
onNewIntent
当一个活动已经在后台,并且您希望在不创建的情况下再次启动它时,调用onNewIntent
有什么区别onCreate
在活动第一次创建时仅被调用一次。谢谢,我认为它解决了我的问题,但我仍然不明白onCreate
之间的区别是当活动已经在后台,并且您希望在不创建的情况下再次启动它时,调用OnEvent
<代码>onCreate在首次创建活动时仅调用一个。