Android 如何在单击按钮时清除片段堆栈和活动堆栈

Android 如何在单击按钮时清除片段堆栈和活动堆栈,android,android-fragments,android-activity,Android,Android Fragments,Android Activity,也有类似的问题,但没有一个能解决我的问题。 我的应用程序流程如下: 活动主页启动活动B(进行设置工作) 在活动B中,三个屏幕作为片段承载。。。 片段1->片段2->片段3 这就是我制作碎片的方法。我没有使用replace,只是添加了它 FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beg

也有类似的问题,但没有一个能解决我的问题。 我的应用程序流程如下:

活动主页启动活动B(进行设置工作) 在活动B中,三个屏幕作为片段承载。。。 片段1->片段2->片段3

这就是我制作碎片的方法。我没有使用replace,只是添加了它

FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        Fragment fragment = null;
        if (getFragType().equals("simple")) {
            fragment = new SimpleFragment().newInstance();
        }
        if (getFragType.equals("inter")) {
            if (getFragType.getComponent().equals("con"))
                fragment = new SetupConFragment().newInstance();
            else if (getFragType.getComponent().equals("ben"))
                fragment = new SetupBenageFragment().newInstance();
        }
        fragmentTransaction.add(R.id.fragment_container, fragment);
        fragmentTransaction.commit();
        }
Fragment3有一个“完成”按钮。因此,一旦碎片3中的所有3个步骤都完成,我将启动一个新的活动C

问题是:-- 在活动C中,如果用户按下后退按钮,则显示片段2,然后显示片段1。理想情况下,一旦用户处于活动C中,按back press键就应该退出屏幕 我已经使用了意图标志的所有组合,但它没有帮助

这是我的按钮点击

Intent launch = new Intent(mContext, MainActivity.class);
                    launch.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    launch.putExtra("Exit me", true);
                    mContext.startActivity(launch);
                    ((ConfigureActivity)mContext).finish();

任何帮助都将不胜感激。

在活动C中,您可以通过覆盖
onBackPressed()
来控制后退键


比如说,如果您想在活动A中启动活动B,但不想让用户返回到活动A

final Intent intent = new Intent(this, ActivityB.class);
startActivity();
finish(); // kill the current activity (i.e., Activity A) so user cannot go back
您可以这样做:

清除碎片堆栈

getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); 
Intent intent = new Intent(Your_Current_Activity.this, Your_Destination_Activity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
清除活动堆栈

getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); 
Intent intent = new Intent(Your_Current_Activity.this, Your_Destination_Activity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

希望这能对您有所帮助。

是的,但这并不能解决这个问题。我可以忽略那里的super.onBackPressed(),但我想删除完整的片段堆栈。对活动B调用finish()应该将其从后堆栈中删除。如果用户按下活动C中的“后退”按钮,焦点应该放在哪里?ConfigureActivity是我的活动B,理想情况下应该是这样。但似乎在某个地方遗漏了一个地方。清除回溯不起作用(Intent.FLAG\u ACTIVITY\u CLEAR\u TOP)我想你还没有看到我的帖子满了,这就是我正在做的……我的问题是清除堆栈。谢谢你,tons Hiren,它起作用了:-)奇怪的是,我在两个不同的语句中给出了相同的选项,但它不起作用,管道符号却起作用了。我刚刚使用了intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK | intent.FLAG_ACTIVITY_CLEAR_TASK),所有相关的片段任务都被清除了。..Voteup并接受答案。@simon很高兴帮助您。