我有一个问题。我有两个活动 - 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);
}
});
湾第二个活动的文本字段很少
当我在ActivityB中单击后退按钮然后应用切换到MainActivity,但我没有完成()ActivityB。
如何在MainActivity上编程按钮,该按钮将检查ActivityB是否已创建。如果是,只需切换到ActivityB(可能在文本字段中有一些类型化的数据),否则创建ActivityB并切换到它???
此时每次点击按钮都会创建新的活动(新的文本字段等等,因此用户在数据之前会松散蠕动)。
我试过
android:launchMode="singleInstance"
和标志FLAG_ACTIVITY_REORDER_TO_FRONT ...
答案 0 :(得分:2)
ActivityA - > ActivityB - > ActivityA(重复使用)
启动ActivityA时,在ActivityB中:
使用FLAG:
/**
* 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是的,这正是你想要的:)
将结果返回给第一个活动(通过附加活动开始活动)