我有一个Activity流程,如下:ACTIVITY A -> ACTIVITY B -> ACTIVITY C -> ACTIVITY D。当用户在Activity D并点击名为“退出”的按钮时,应用程序应返回到Activity B并结束Activities C和D。我该怎么做?
注意:ACTIVITY B和ACTIVITY D是相同的类但不同的实例。
注意:ACTIVITY B和ACTIVITY D是相同的类但不同的实例。
<activity android:name=".xx.xx.ActivityA"
android:noHistory="false"
android:screenOrientation="nosensor">
</activity>
<activity android:name=".xx.xx.ActivityB"
android:noHistory="true"
android:screenOrientation="nosensor">
</activity>
<activity android:name=".xx.xx.ActivityC"
android:noHistory="true"
android:screenOrientation="nosensor">
</activity>
<activity android:name=".xx.xx.ActivityD"
android:noHistory="true"
android:screenOrientation="nosensor">
</activity>
按钮
:exitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),ActivityB.class);
startActivity(intent);
finish();
}
});
When you back from activity D and go to activity A then you need to use these code.
1. First create the instance of all activity using this code.
Activity A{
}
Activity B{
public static Activity activityB;
oncreate(){
activityB=this;
}
Activity C{
public static Activity activityC;
oncreate(){
activityC=this;
}
Activity D{
}
2. Please call this method on back press in Activity D
private finishActivity(){
Clear the instance of middle activity.
B.activityB.finish();
C.activityC.finish();
finish();
}
这是一个相当复杂的解决方案,但允许完全操纵后退栈。基本上,在你的Application
中创建自己的“后退栈”:
public class MyApplication extends Application {
private Set<Activity> runningActivities = new HashSet<>();
onCreate() {
// ...
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
@Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
runningActivities.add(activity);
}
@Override public void onActivityDestroyed(Activity activity) {
runningActivities.remove(activity);
}
});
}
public void killActivities(Set<Class<?>> activitiesToRemove) {
for (Activity stackActivity : runningActivities) {
if (activitiesToRemove.contains(stackActivity.getClass())) {
stackActivity.finish();
}
}
}
}
((MyApplication)getApplication()).killActivities(ActivityD.class, ActivityC.class)
class MyApplication : Application() {
private val runningActivities = mutableSetOf<Activity>()
override fun onCreate() {
// ...
registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle) {
runningActivities.add(activity)
}
override fun onActivityDestroyed(activity: Activity) {
runningActivities.remove(activity)
}
})
}
fun killActivities(activitiesToRemove: Set<Class<Activity>>) {
runningActivities.forEach { activity ->
if (activitiesToRemove.contains(activity.javaClass)) {
activity.finish()
}
}
}
}
只需将此添加到您的ActivityD.class中
finishButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),ActivityB.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
});
希望能对你有所帮助!
最佳和完全可靠的实现方法是创建扩展android.app.Application的Application类,并在那里维护已打开活动实例的列表。
接下来,在AndroidManifest.xml中的<application>
中提及您创建的Application类名称
然后,每当您需要时,可以销毁已打开的活动。