【问题标题】:Android - Open already created ActivityAndroid - 打开已经创建的 Activity
【发布时间】:2016-01-08 23:29:25
【问题描述】:

我有一个问题。我有两个活动 - MainActivity 和 ActivityB

一个。第一个活动(MainActivity)有一个启动 ActivityB 的按钮

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent activityB = new Intent(MainActivity.this, activityB.class);
                startActivity(activityB);
            }
        });

b.第二个活动的文本字段很少

当我单击 ActivityB 中的后退按钮时,应用程序切换到 MainActivity,但我没有完成() ActivityB。

如何对 MainActivity 上的按钮进行编程,该按钮将检查 ActivityB 是否已创建。如果是,只需切换到 ActivityB(可能在文本字段中输入一些数据),否则创建 ActivityB 并切换到它???

此时每次点击按钮都会创建新的活动(新的文本字段等,因此用户在数据之前松散地写了)。

我试过了

android:launchMode="singleInstance"

和标志 FLAG_ACTIVITY_REORDER_TO_FRONT...

【问题讨论】:

  • which will check if ActivityB was already created. If yes just switch to ActivityB。使用singleInstance 可能会让你实现这一点,但如果使用实例,你的MainActivity 将被弹出(完成)。这是你想要的吗?

标签: java android android-intent android-activity


【解决方案1】:

ActivityA -> ActivityB -> ActivityA(重用)

在ActivityB中同时启动ActivityA:

  • 使用标志:

        /**
         * When an existing singleTask activity is launched, 
         * all other activities above it in the stack will be destroyed.
         * It's different when launchMode is "Standard". 
         * The task which contains flag will come to the foreground 
         * and keep the state the same as before.
         * When you press HOME and launch the activity again, 
         * ActivityManger calls an intent
         *     {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER]
         *      flag=FLAG_ACTIVITY_NEW_TASK|FLAG_ACTIVITY_RESET_IF_NEEDED cmp=A}    
         */
    
     Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP;
    
  • 设置附加功能或操作

并在 ActivityA 中覆盖:

  /**
 * This is called for activities that set launchMode to "singleTop" in
 * their package, or if a client used the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP}
 * flag when calling {@link #startActivity}.  In either case, when the
 * activity is re-launched while at the top of the activity stack instead
 * of a new instance of the activity being started, onNewIntent() will be
 * called on the existing instance with the Intent that was used to
 * re-launch it.
 *
 * <p>An activity will always be paused before receiving a new intent, so
 * you can count on {@link #onResume} being called after this method.
 *
 * <p>Note that {@link #getIntent} still returns the original Intent.  You
 * can use {@link #setIntent} to update it to this new Intent.
 *
 * @param intent The new intent that was started for the activity.
 *
 * @see #getIntent
 * @see #setIntent
 * @see #onResume
 */
@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
    /** when we are back check here intent action for example or extras  */
}

我不是这个意思。请再读一遍:我如何在 MainActivity 上对按钮进行编程,该按钮将检查 ActivityB 是否已创建。如果是,只需切换到 ActivityB(可能在文本字段中有一些键入的数据),否则创建 ActivityB 并切换到它??? - Juliusz Hajdacki

@juliusz-hajdacki 是的,这正是你想要的 :)

  1. 第一个带有按钮的活动
  2. 按钮监听器使用标志启动活动
  3. 检查是否在第二个活动中点击了 onNewIntent 方法或 onCreate
  4. 将结果返回到第一个活动(通过带有附加功能的开始活动)

    • 请再仔细阅读一遍:)

【讨论】:

  • 我不是这个意思。请再读一遍:我如何在 MainActivity 上对按钮进行编程,该按钮将检查 ActivityB 是否已创建。如果是,只需切换到 ActivityB(可能在文本字段中输入一些数据),否则创建 ActivityB 并切换到它???
  • @juliusz-hajdacki 是的,这正是你想要的 :) 使用按钮的第一个活动 -> 使用标志启动活动 -> 检查 onNewIntent 方法是否被点击或 onCreate -> 将结果返回给第一个活动(通过用额外的东西开始活动)-再仔细阅读一次:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-04
  • 2016-12-05
相关资源
最近更新 更多