Android Fragment 如何清空回退栈中的Fragment

因为项目需求,需要在最后关机的时候清空所有回退栈中的Fragment。

当时的想法就是找到一个Fragment的方法或者是FragmentManger的方法,然后调用这个方法,将其清空。


接下来就是照这个方法,终于找到一个方法getSupportFragmentManager().getFragments().clear();

然后就是尝试,把代码加进去

if (getSupportFragmentManager().getFragments() != null
&& getSupportFragmentManager().getFragments().size() > 0) {
getSupportFragmentManager().getFragments().clear();



结果是可以清空的。但是问题也来了,程序总是崩溃,报一个数组超标的错误,然后就是调试

怎么也找不到错误所在,因为在清空Fragment代码中压根就没有数组(arraylist),知道最后就是

source not found。郁闷了。


这个问题暂且搁下,另找方法。重新生成Fragment实例吧,看看成不成。上代码


               mAutoMassageLayout = (FrameLayout) findViewById(R.id.layout_auto);
mManualMassageLayout = (FrameLayout) findViewById(R.id.layout_manual);
mHolographicHandMassageLayout = (FrameLayout) findViewById(R.id.layout_holographic_hand);
mOtherMassageLayout = (FrameLayout) findViewById(R.id.layout_other);
mPositionAdjustLayout = (FrameLayout) findViewById(R.id.layout_ajust);


mImageAuto = (ImageView) findViewById(R.id.img_auto);
mImageManual = (ImageView) findViewById(R.id.img_manual);
mImageHolographicHand = (ImageView) findViewById(R.id.img_holographic_hand);
mImageOther = (ImageView) findViewById(R.id.img_other);
mImagePosition = (ImageView) findViewById(R.id.img_position);


mTextAuto = (TextView) findViewById(R.id.tv_auto);
mTextManual = (TextView) findViewById(R.id.tv_manual);
mTextHolo = (TextView) findViewById(R.id.tv_holo);
mTextOther = (TextView) findViewById(R.id.tv_other);
mTextPosition = (TextView) findViewById(R.id.tv_position);


mAutoMassageLayout.setOnClickListener(this);
mManualMassageLayout.setOnClickListener(this);
mHolographicHandMassageLayout.setOnClickListener(this);
mOtherMassageLayout.setOnClickListener(this);
mPositionAdjustLayout.setOnClickListener(this);


// if (getSupportFragmentManager().getFragments() != null
// && getSupportFragmentManager().getFragments().size() > 0) {
// getSupportFragmentManager().getFragments().clear();
// Log.d("--------------//", (getSupportFragmentManager()
// .getFragments() != null)
// + ", "
// + getSupportFragmentManager().getFragments().size());
// }
mFragmentMainNav = new FragmentMainNav();
mAutoMassageFragment = new FragmentAutoMassage();
mManualMasssageFragment = new FragmentManualMassage();
mHolographicHandMassageFragment = new FragmentHolographicHandMassage();
mOtherMassageFragment = new FragmentOtherMassage();
mSettingsFragment = new FragmentSettings();
mServicesFragment = new FragmentServices();
mStateFragment = new FragmentState();
}

试了一下,成功了,暂时没发现什么问题。虽然是个笨办法,但是功能好使就好。

如果还有什么好方法,请大家继续分享吧。


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值