gpt4 book ai didi

java - Android - 打开已经创建的 Activity

转载 作者:太空狗 更新时间:2023-10-29 13:15:56 24 4
gpt4 key购买 nike

我有一个问题。我有两个 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);
}
});

第二个 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 */
}

Thats not what I mean. Please read one more time: How can I programme button on MainActivity which will check if ActivityB was already created. If yes just switch to ActivityB (maybe with some typed data in text fields), else create ActivityB and switch to it ??? - Juliusz Hajdacki

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

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

    • 请仔细阅读一遍:)

关于java - Android - 打开已经创建的 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34687808/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com