Android:如何切换到已创建的“活动" [英] Android: How to switch to Activity that has already been created

查看:127
本文介绍了Android:如何切换到已创建的“活动"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android编程的新手. 我要做的是将另一个活动创建完毕. 假设我启动了活动B,然后从活动A移至活动B,然后按了后退按钮,然后又移回活动A. 现在,我想在完成倒数计时器后切换到活动B.

I'm new to Android programming. What I want to do is to swith to an another activity that has already been created. Suppose I started Activity B and move from Activity A to Activity B, and then I pressed back button and move back to Activity A. Now I want to switch to Activity B when a countdown timer is done.

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();
}

我该怎么办?

推荐答案

来自

FLAG_ACTIVITY_REORDER_TO_FRONT

public static final int FLAG_ACTIVITY_REORDER_TO_FRONT

如果在传递给Context.startActivity()的Intent中进行设置,则此标志将 使启动的活动被带到任务的最前面 历史记录堆栈(如果已在运行).

If set in an Intent passed to Context.startActivity(), this flag will cause the launched activity to be brought to the front of its task's history stack if it is already running.

例如,考虑一个由四个活动组成的任务:A,B,C, D.如果D使用解析为Intent的Intent调用startActivity() 活动B的组成部分,那么B将被带到活动的最前面 历史记录堆栈,其结果顺序为:A,C,D,B.此标志将 如果还指定了FLAG_ACTIVITY_CLEAR_TOP,则将被忽略.

For example, consider a task consisting of four activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then B will be brought to the front of the history stack, with this resulting order: A, C, D, B. This flag will be ignored if FLAG_ACTIVITY_CLEAR_TOP is also specified.

根据您的情况,您可以在ActivityAActivityB之间切换,而无需完成或重新创建它们.

In your case, you can switch between ActivityA and ActivityB without finishing or recreating them.

放在一起.

活动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
}

这篇关于Android:如何切换到已创建的“活动"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆