简体   繁体   中英

Android savedInstanceState return always null

I have FragmentA and FragmentB, i'm in a FragmentB when i go back to FragmentA i need load last value but my savedInstanceState return always null:

public static class PlaceholderFragment extends Fragment {

        private boolean active=false;



        @Override
        public void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            outState.putBoolean("active",active);

        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);



        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

            if(savedInstanceState!=null)
            {

                active=savedInstanceState.getBoolean("active");
                if(attivo==true)
                {
                    Toast.makeText(getActivity(),"True",Toast.LENGTH_SHORT).show();
                }
                else
                {
                   Toast.makeText(getActivity(),"False",Toast.LENGTH_SHORT).show();

                }
            }

        }

to pass in FragmentB i use:

getFragmentManager().beginTransaction().replace(R.id.container,new PrefFragment()).addToBackStack("back").commit();

The Bundle savedInstanceState will always return null in this particular circumstance because it is only passed in if your fragment is destroyed and re-created by the android system (eg when the screen is rotated or your app is destroyed due to low system resources). The savedInstanceState is not used to store data persistently in an app, if you want to do that, I suggest using SharedPreferences:

private void saveBooleanToPreferences(String key, boolean bool){
     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
     SharedPreferences.Editor editor = preferences.edit();
     editor.putBoolean(key, bool);
     editor.apply();
}

private boolean getBooleanFromPreferences(String key){
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    boolean bool = preferences.getBoolean(key, false);
    return bool;
}

Therefore, your code will be something like:

 public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean("active",active);
        saveBooleanToPreferences("active",active);
    }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

            if(savedInstanceState!=null)
            {

                active=getBooleanFromPreferences("active")
                if(attivo==true)
                {
                    Toast.makeText(getActivity(),"True",Toast.LENGTH_SHORT).show();
                }
                else
                {
                   Toast.makeText(getActivity(),"False",Toast.LENGTH_SHORT).show();

                }
            }

确保包含“片段”的活动未覆盖onSaveInstanceState(或至少调用super方法)。

使用sharedPreferences将数据从片段B传递到片段A。

Wrong , if you are pressing back Without Handling the onBackPressed() then ur application is closed without saving any instance, but if you press home the save will occure . remember on handling on backPressed, remove its super

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM