如何阻止碎片被破坏? [英] How can I stop my fragment from being destroyed?

查看:56
本文介绍了如何阻止碎片被破坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个导航视图,其中包含一些项,当按下一项时,它将转到该片段.例如,如果您按下抽屉上的主页"项目,它将弹出主页片段.

I have a navigation view with some items in it, and when one item is pressed it will go to that fragment. For example, if you press the "Home" item, on the drawer, it will bring up the home fragment.

        navigationView = (NavigationView)findViewById(R.id.navigation_view);
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.home_id:
                    HomeFragment homeFragment = (HomeFragment) fragmentManager.findFragmentByTag("Home");
                    if (homeFragment == null)
                        homeFragment = new HomeFragment();
                    fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.Main, homeFragment, "Home");
                    fragmentTransaction.commit();
                    getSupportActionBar().setTitle("Home");
                    item.setChecked(true);
                    drawerLayout.closeDrawers();
                    break;

                case R.id.chat_id:
                    ChatFragment chatFragment = (ChatFragment) fragmentManager.findFragmentByTag("Chat");
                    if (chatFragment == null)
                        chatFragment = new ChatFragment();
                    fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.Main, chatFragment, "Chat");
                    fragmentTransaction.commit();
                    getSupportActionBar().setTitle("Chat");
                    item.setChecked(true);
                    drawerLayout.closeDrawers();
                    break;
            }

        }
    }

所以我认为正在发生的事情是,当我使用replace()方法时,它正在破坏聊天片段.如果我有关于聊天片段的数据,并且我想确保在更改片段时数据不会消失,该怎么办.

So what I think is happening is that when I am using the replace() method, it is destroying the chat fragment. What if I have data on the chat fragment, and I want to make sure the data does not go away when I change fragments.

ChatFragment类中,我放置了此覆盖方法:

In the ChatFragment class I have put this override method:

@Override
public void onSaveInstanceState(Bundle outState)
{
    Log.d("CHATFRAG", "onSaveInstanceState: been called ");
    super.onSaveInstanceState(outState);
    outState.putString("textView",textView.getText().toString());
    //Save the fragment's state here
}

现在,当我在应用程序中的片段之间切换时,不会调用此方法,这意味着片段已被销毁.

Now when I switch between fragments in my app, this method does not get called, which means the fragment is being destroyed.

是否有另一种方法可以与所需的片段交换屏幕上的当前片段(而不破坏两者)? 我将有更多的片段(最多十个),是否有一个不需要重复代码的优化解决方案?

Is there an alternative way where I can swap the current fragment that is on screen, with the desired fragment(without destroying both)? I will be having more fragments (up to ten), is there an optimised solution that will not require duplicate code?

推荐答案

您正在用其他片段替换片段,因此第一个片段将被销毁.如果您不想破坏它,则需要add而不是replace

you are replacing fragment with other, so first one will get distroyed. If you dont want it to destroy you need to add it instead of replace

fragmentTransaction.add(R.id.Main, homeFragment, "Home");

这篇关于如何阻止碎片被破坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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