我有问题我有两个 Activity -MainActivity和ActivityB

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

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

b。第二 Activity 没有几个文本字段

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

我如何编程MainActivity上的按钮,它将检查是否已创建ActivityB。如果是,则仅切换到ActivityB(也许在文本字段中输入一些键入的数据),否则创建ActivityB并切换到它?

此时,每次单击按钮都会创建新的 Activity (新的文本字段等,因此用户在数据之前会先写一些)。

我试过了
android:launchMode="singleInstance"

并标记FLAG_ACTIVITY_REORDER_TO_FRONT ...

最佳答案

ActivityA-> ActivityB-> ActivityA(重复使用)

在启动ActivityA时在ActivityB中:

  • 使用标志:
        /**
         * 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是的,这正是您想要的:)
  • 第一个 Activity ,带有按钮
  • 按钮侦听器以标志
  • 开始 Activity
  • 检查第二个 Activity
  • 中的onNewIntent方法是否被击中或onCreate
  • 返回结果到第一个 Activity (通过带有附加功能的开始 Activity )
  • 请再仔细阅读一次时间:)
  • 关于java - Android-打开已创建的 Activity ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34687808/

    10-11 15:05