1

First I'm still new to this android programming. I want to ask, why my switch always turn on when i start my app. When I turn it off and go to another activity and back to the first activity, the switch button turn on again Any clue ? I tried Android ToggleButton always check but i still don't get it.

    switchStatus = (TextView) findViewById(R.id.View1);
    mySwitch.setChecked(true);
    mySwitch = (Switch) findViewById(R.id.switch1);  

    mySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {

     @Override
     public void onCheckedChanged(CompoundButton buttonView,
     boolean isChecked) {

      if(mySwitch.isChecked()){
        mySwitch.setChecked(true);
        switchStatus.setText("Switch is currently On");
        onResume();          
        sendButton.setOnClickListener(new OnClickListener() {   
        @Override
        public void onClick(View arg0) {
        }
        });        
      }
      else{
       mySwitch.setChecked(false);
       switchStatus.setText("Switch is currently OFF");
       onPause();
      }
      }
      });
Community
  • 1
  • 1
noob
  • 11
  • 4

4 Answers4

4

The switch state is lost, because the Activity is destroyed when you leave it. So you should consider saving state of mySwitch. You can use SharedPreferences to acomplish this, or ...

you should use the onPause() method to write any persistent data (such as user edits) to storage. In addition, the method onSaveInstanceState(Bundle) is called before placing the activity in such a background state, allowing you to save away any dynamic instance state in your activity into the given Bundle, to be later received in onCreate(Bundle) if the activity needs to be re-created. See the Process Lifecycle section for more information on how the lifecycle of a process is tied to the activities it is hosting. Note that it is important to save persistent data in onPause() instead of onSaveInstanceState(Bundle) because the latter is not part of the lifecycle callbacks, so will not be called in every situation as described in its documentation.

Álvaro Pérez Soria
  • 1,443
  • 1
  • 13
  • 26
1

You can add sharedPreference to save the value when you clicked the switch to avoid losing the value. You may try this code, though this is not yet tested.

SharedPreferences prefs; // declare the sharedPreference
boolean value = false; // default value if no value was found
String key = "key"; // use this key to retrieve the value
String sharedPrefName = "isMySwitchChecked"; // name of your sharedPreference

prefs = context.getSharedPreferences(sharedPrefName, Context.MODE_PRIVATE);
value = prefs.getBoolean(key, value); // retrieve the value of your key

switchStatus = (TextView) findViewById(R.id.View1);
mySwitch = (Switch) findViewById(R.id.switch1);
mySwitch.setChecked(value);

mySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

if(mySwitch.isChecked()){

switchStatus.setText("Switch is currently On");
prefs = context.getSharedPreferences(sharedPrefName, Context.MODE_PRIVATE);
prefs.edit().putBoolean(key, true).commit();    
onResume();          

sendButton.setOnClickListener(new OnClickListener() {   

@Override
public void onClick(View arg0) { }

});        

} else {

prefs = context.getSharedPreferences(sharedPrefName, Context.MODE_PRIVATE);
prefs.edit().putBoolean(key, false).commit();
switchStatus.setText("Switch is currently OFF");
onPause();

  }  
 }
});
mgcaguioa
  • 1,443
  • 16
  • 19
  • why when I try to run in my phone, the app just force close ? btw, i used android KitKat – noob Jan 07 '15 at 17:33
  • i dunno because there is no error in my line of code, it's like my activity can create the xml – noob Jan 08 '15 at 14:59
0

I assume that you haven't learned about fragments yet, and still uses activities. Study about the activity life cycle and use fragments.

This is why your activity always show your switch as true.

mySwitch.setChecked(true);

Omatt
  • 8,564
  • 2
  • 42
  • 144
0

Set your switech off by using this => mySwitch = (Switch) findViewById(R.id.switch1); mySwitch.setChecked(false); //set False Instead of true
now when you start your activity switch will be off.

Chirag Savsani
  • 6,020
  • 4
  • 38
  • 74
  • but when i go to another activity the switch button go back again the same as before, how do I keep my switch button value even when i go to another activity ? – noob Jan 07 '15 at 18:40