Android片段回压,没有任何数据丢失 [英] Android fragment back press without any data loss

查看:68
本文介绍了Android片段回压,没有任何数据丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击每个导航列表时,我将使用导航抽屉创建一个应用程序,新片段将加载.片段之一具有listView.当我单击此列表时,加载另一个片段.现在,我使用导航抽屉移至不同的片段.每次想返回上一个片段而没有任何数据更改时,我都按后退按钮.该怎么办?

I am creating an application with navigation drawer when I click each navigation list new fragments will load. One of the fragment have listView. When I click this list load another fragment. Now I move to different fragments using navigation drawer. When I press back button every time I want to move to previous fragment without any data change.How can I do it?

我正在使用以下代码加载片段:

I am loading fragment using the following code:

FragmentManager fm = getFragmentManager();
        for (int i = 0; i < fm.getBackStackEntryCount(); i++) {
            fm.popBackStack();
        }
    FragmentTransaction fragmentTransaction = fm.beginTransaction();
            fragmentTransaction.replace(R.id.fragmentHome, fr);
            fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            fragmentTransaction.commit();

简而言之::每按一次回退键,我都希望加载前一个片段而无需重新创建它

推荐答案

我认为这可能有帮助

只需执行 Fragmentmanager

FragmentManager fm = getFragmentManager();
        for (int i = 0; i < fm.getBackStackEntryCount(); i++) {
            fm.popBackStack();
        }

在那个片段中

View mView ; //this will be global

@Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        try {
            if (mView == null) {
                // view will be initialize for the first time .. you can out condition for that if data is not null then do not initialize view again. 
                mView = inflater.inflate(R.layout.xml,container, false);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return mView;
    }

这篇关于Android片段回压,没有任何数据丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆