user1608228
user1608228

Reputation: 249

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();

Upvotes: 2

Views: 3720

Answers (4)

Reza Baiat
Reza Baiat

Reputation: 33

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

Upvotes: 0

Andre Perkins
Andre Perkins

Reputation: 7800

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 (e.g. 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();

                }
            }

Upvotes: 1

Raj
Raj

Reputation: 996

use sharedPreferences to pass the data from your fragment B to fragment A .

Upvotes: 0

pdegand59
pdegand59

Reputation: 13019

Make sure that the activity containing the Fragments does not override onSaveInstanceState (or at least call super method).

Upvotes: 0

Related Questions