从bottomNavigationView导航到片段B时保存片段A的状态

问题描述 投票:0回答:1

我是Android的新手,现在我一直在努力保存片段状态。我的应用程序的bottomNavigationView中有5个选项(我正在使用Android的默认活动)。在片段A(实际上称为“ SearchFragment”)中,我有一个按钮,单击该按钮可在TextBox中设置一些文本。当我导航到另一个片段并返回到片段A时,我想保存该片段的状态(文本在那里),并从上一个按钮单击以使文本仍然在那里。

根据我编写的代码,例如,当更改方向时,它会保存片段的状态,但导航到其他片段然后返回时不会保存片段的状态。

我如何更改代码?还是有其他方法可以在导航中保存片段的当前状态?我真的需要这里的帮助...感谢您的关注!

我的mainActivity看起来像这样:

    public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            BottomNavigationView navView = findViewById(R.id.nav_view);
            // Passing each menu ID as a set of Ids because each
            // menu should be considered as top level destinations.

            AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                    R.id.navigation_search, R.id.navigation_explore, R.id.navigation_trips, R.id.navigation_groups,R.id.navigation_profile)
                    .build();

            NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
            NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
            NavigationUI.setupWithNavController(navView, navController);
        }

    }

片段类看起来像这样:


    public class SearchFragment extends Fragment {

        private SearchViewModel searchViewModel;
        Button b;
        TextView text;
        Bundle savedState = null;

        public View onCreateView(@NonNull LayoutInflater inflater,
                                 ViewGroup container, Bundle savedInstanceState) {

            View root = inflater.inflate(R.layout.fragment_search, container, false);

            searchViewModel = ViewModelProviders.of(this).get(SearchViewModel.class);
            final TextView textView = root.findViewById(R.id.text_dashboard);
            searchViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
                @Override
                public void onChanged(@Nullable String s) {
                    textView.setText(s);
                }
            });
            Log.i("state","onCreate");
            Log.i("bundleIsNull", "" + (savedState == null));


            b  = root.findViewById(R.id.button2);
            text = root.findViewById(R.id.textView);


            b.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    text.setText("YEEEY");
                    Log.i("bundleIsNull", "" + (savedState == null));
                }
            });


            if(savedInstanceState != null && savedState == null) {
                savedState = savedInstanceState.getBundle("buttonText");
            }
            if(savedState != null) {
                text.setText(savedState.getCharSequence("buttonText"));
            }
            savedState = null;

            return root;
        }


        @Override
        public void onDestroyView() {
            super.onDestroyView();
            Log.i("state", "onDestroyView");
           savedState = saveState();
           text = null;
           b = null;
        }

        private Bundle saveState() {
            Bundle state = new Bundle();
            state.putCharSequence("buttonText", text.getText());
            return state;
        }

        @Override
        public void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            Log.i("state", "onSaveInstanceState");
            outState.putBundle("buttonText", (savedState != null)? savedState : saveState());
        }

        @Override
        public void onPause() {
            super.onPause();
            Log.i("state","onPause");
        }
    }

android android-fragments uinavigationbar bottomnavigationview onsaveinstancestate
1个回答
0
投票

->片段总是被重新创建(一旦用户导航到另一个片段,就会调用onCreate,onViewCreated,onViewDestroyed)

->片段仅在重新创建活动(例如屏幕旋转)时才保存其状态,在片段之间导航不会保存片段的状态。

我想保存此片段的状态(文本在那里)导航到另一个片段,然后回到片段A(并通过单击上一个按钮将文本保留在此处)。

这不可能,因为当创建FragmentA的新实例时您从fragmentB导航回到Fragment A。

到目前为止,您无法使用导航控制器实现此功能。

因为您正在使用

导航组件,所以应该切换到使用

Viewmodel解决保留片段状态的问题。

© www.soinside.com 2019 - 2024. All rights reserved.