打开关闭其前一个活动的新活动,而不关闭其前一个活动

时间:2016-12-16 10:12:30

标签: android android-intent

如果我有各种活动 - A,B,C,D,E,F。 a A打开B,B打开C,C打开D,依此类推A-> B-> C-> D-> E-> F。
目前我在开幕后的所有活动中都参加了F活动。

现在我想从F开始进行B活动而不关闭A但关闭所有活动(C,D,E,F)。请告诉我们如何做到这一点?

3 个答案:

答案 0 :(得分:1)

试试这个。

  1. 活动A调用B(完成活动)
  2. 活动B调用C(完成B活动) 使用B活动中的onBackPressed方法调用活动
  3. 活动C调用D(完成C活动) 使用D活动中的onBackPressed方法调用C活动
  4. 活动D调用E(完成D活动) 使用E活动中的onBackPressed方法调用D活动
  5. 活动E调用F(完成E活动) 使用F活动中的onBackPressed方法调用B活动

答案 1 :(得分:0)

在这种情况下,你必须使用Intent.FLAG_ACTIVITY_CLEAR_TOP

Intent intent - new Intent(this, B.class);
intent.setFlag(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

来自文档:

  

int FLAG_ACTIVITY_CLEAR_TOP

     

如果设置,则正在启动的活动已在运行中   当前任务,然后而不是启动它的新实例   活动,其上的所有其他活动将被关闭   这个意图将作为一个传递到(现在在顶部)旧活动   新的意图。

答案 2 :(得分:0)

您应该使用MainActivity和多个片段进行这些操作。

首先,创建一个Fragment容器(基本上,一个可以容纳Fragment的视图,您可以随时从MainActivity更改该Fragment)。

您的MainActivity的XML布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />


然后在MainActivity中更改容器中的片段,如下所示:

// Create a new Fragment to be placed in the activity layout
YourFragment firstFragment = new YourFragment();

// In case this activity was started with special instructions from an
// Intent, pass the Intent's extras to the fragment as arguments
firstFragment.setArguments(getIntent().getExtras());

// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
     .add(R.id.fragment_container, firstFragment).commit();
相关问题