dharmendra vaishnav
dharmendra vaishnav

Reputation: 2111

Get boolean from savedInstancestate on orientatioin change

I want to save some boolean variables in Bundle i.e savedInstaceState those i can use after orientation changed .

I tried but always the savedInstanceState result in null

if(savedInstanceState==null){
 savedInstanceState=new Bundle();
 savedInstanceState.putBoolean("isLogoLoaded",true);
 }else{
 savedInstanceState.putBoolean("isLogoLoaded",true);
}  

please provide the better way thanks .

Upvotes: 0

Views: 3549

Answers (3)

mlaHackingIsMagic
mlaHackingIsMagic

Reputation: 271

Which method are you calling that logic?

I am guessing that is in onCreate(), onCreate is where you LOAD the savedInstanceState, it is NOT where you SAVE your data. (you can also load it in onRestoreInstanceState)

You want to save your data in public void onSaveInstanceState(Bundle savedInstanceState) {

 @Override
 public void onSaveInstanceState(Bundle savedInstanceState) {
     super.onSaveInstanceState(savedInstanceState);
     savedInstanceState.putBoolean("isLogoLoaded", true);
 }

From here http://developer.android.com/training/basics/activity-lifecycle/recreating.html

Upvotes: 1

Ashish Tamrakar
Ashish Tamrakar

Reputation: 820

You can use below code for saving the values which you want to restore -

protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("TEXT", user);

    }

By this you can get those values back -

protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    savedUser = savedInstanceState.getString("TEXT");
    Log.d("enregistred value", savedUser);

Hope this helps :)

}

Upvotes: 2

Sanjay Hirani
Sanjay Hirani

Reputation: 406

You need to use

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        savedInstanceState.putBoolean("isLogoLoaded",true);
}

and

public void onRestoreInstanceState(Bundle savedInstanceState) {
          super.onRestoreInstanceState(savedInstanceState);
          logo = savedInstanceState.getBoolean("isLogoLoaded"); 
}

Upvotes: 1

Related Questions