Randroid
Randroid

Reputation: 3698

savedpreferences not working on reboot

Shared preferences inside tabhost not working for button pressed state.

I am changing the background of the button on pressed state. But when I reboot(off and on) the phone the shared preferences is not saving the state.

refereed from here

Shared preferences for button pressed state inside tabhost not working on reboot

on debugging, isclick is showing as true in if-else condition. I dont understand the problem.

for the first time when I run, I am getting the button with highlighted background and even on reboot also i am getting the same highlited background.

Any help is always appreciated.

private boolean isclick ;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    seatdirnbtn = (Button) findViewById(R.id.seatdirnbtn);
    prefs = this.getSharedPreferences(prefName, MODE_PRIVATE);

    isclick = prefs.getBoolean("prefName", false);
    System.out.println("bool? " + isclick);

    if (isclick) {
        seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
    } else if (!isclick) {
        seatdirnbtn.setBackgroundResource(R.drawable.icon4);
    } 
}


@Override
public void onRestart() {
    super.onRestart();

    if (isclick) {
        seatdirnbtn.setBackgroundResource(R.drawable.icon4);
    } else if (!isclick) {
        seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
    }
}

@Override
public void onStop() {
    super.onStop();

    prefs = this.getSharedPreferences(prefName, MODE_PRIVATE);
    editor = prefs.edit();

    editor.putBoolean("prefName", true);
    editor.commit();
}

private View.OnClickListener listner1 = new View.OnClickListener() {
    public void onClick(View v) {
        if (isclick) {
            seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);

            editor = prefs.edit();
            editor.clear();
            editor.putBoolean("prefName", true);
            editor.commit();
        } else if (!isclick) {
            seatdirnbtn.setBackgroundResource(R.drawable.icon4);
            editor = prefs.edit();
            editor.clear();
            editor.putBoolean("prefName", false);
            editor.commit();
        }
        isclick = !isclick;
    }
};

EDIT1:

    private boolean isclick  ;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    prefs = this.getSharedPreferences(prefName, MODE_PRIVATE);

    isclick = prefs.getBoolean("prefName", true);

    System.out.println("bool? " + isclick);
    Log.i(TAG, " prefname");

    if (isclick) {
        seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
    } else if (!isclick) {
        seatdirnbtn.setBackgroundResource(R.drawable.icon4);
    } 

         public void onPause() {
    super.onPause();
    prefs = this.getSharedPreferences(prefName, MODE_PRIVATE);
    // boolean isclick = false;
    editor = prefs.edit();
    // editor.clear();
    editor.putBoolean("prefName", false);
    Log.i(TAG, " prefname");
    editor.commit();
}

@Override
public void onRestart() {
    super.onRestart();
      prefs = this.getSharedPreferences(prefName, MODE_PRIVATE);
        isclick = prefs.getBoolean("prefName", !isclick);

}

@Override
public void onStop() {
    super.onStop();
    getApplicationContext().unbindService(this);

      prefs = this.getSharedPreferences(prefName, MODE_PRIVATE); // boolean
      isclick = false; editor = prefs.edit(); // editor.clear();
      editor.putBoolean("prefName", !isclick); 
      editor.commit();

      }

         private View.OnClickListener listner1 = new View.OnClickListener() {

    public void onClick(View v) {


        if (isclick) {
            seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);

            editor = prefs.edit();
            editor.clear();
            editor.putBoolean("prefName", false);
            editor.commit();
            isclick = false;
        } else if (!isclick) {
            seatdirnbtn.setBackgroundResource(R.drawable.icon4);
            editor = prefs.edit();
            editor.clear();
            editor.putBoolean("prefName", true);
            editor.commit();
            isclick = true;
        }

    }
};

Upvotes: 0

Views: 149

Answers (1)

Pratik Sharma
Pratik Sharma

Reputation: 13415

Try with this Edited Solution :

public class SharedprefsActivity extends Activity  {

    protected static final String TAG = "HvacActivity";
    /** Called when the activity is first created. */
    private Button seatdirnbtn;
    private SharedPreferences prefs;
    private String prefName = "MyPref";
    private SharedPreferences.Editor editor;
    private boolean isclick;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        seatdirnbtn = (Button) findViewById(R.id.seatdirnbtn);      
        seatdirnbtn.setOnClickListener(listner1);       
    }

    public void onResume() {
        super.onResume();
        getPrefAndButtonState();        
    }

    public void onPause() {
        super.onPause();
        setPrefAndButtonState();
    }

    @Override
    public void onRestart() {
        super.onRestart();
        getPrefAndButtonState();
    }

    @Override
    public void onStop() {
        super.onStop();
        setPrefAndButtonState();
    }

    public void getPrefAndButtonState(){
        prefs = this.getSharedPreferences(prefName, MODE_PRIVATE);
        isclick = prefs.getBoolean("prefName", false);
        System.out.println("bool? " + isclick);
        if (isclick) {
            seatdirnbtn.setBackgroundResource(R.drawable.icon4hlt);
        } else if (!isclick) {
            seatdirnbtn.setBackgroundResource(R.drawable.icon4);
        }
    }

    public void setPrefAndButtonState(){
        editor = prefs.edit();
        editor.putBoolean("prefName", isclick);
        editor.commit();        
        getPrefAndButtonState();
    }

    private View.OnClickListener listner1 = new View.OnClickListener() {    
        public void onClick(View v) {   
            if (isclick) {
                isclick = false;
                setPrefAndButtonState();            
            } else if (!isclick) {
                isclick = true;
                setPrefAndButtonState();
            }   
        }
    };
}

I have test well and it works well for me.

Hope it helps you.

Thanks.

Upvotes: 2

Related Questions