简体   繁体   中英

How to save the togglebutton state on backpressed

I want to save the state of togglebutton so that service can run by checking the state of togglebutton while activity is not active. This is coding of Activity class. In this whatever user save to togglebtn the service must run as per that.

public class MainActivity extends Activity implements OnClickListener {

    ToggleButton tgl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tgl=(ToggleButton)findViewById(R.id.toggleButton1);
        tgl.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        if(tgl.isChecked()){
            Intent i=new Intent(MainActivity.this,Run.class);
            startService(i);
        }else{

        }
    }
    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        if(tgl.isChecked()){
            tgl.setChecked(true);
        }else{

        }

        super.onBackPressed();
    }

}

I suggest to keep the state of toggle button into preferences and retrieve them when you need it(here on back press). I hope you get an idea from following code:

To save:

@Override
public void onClick(View v) 
{
    if (toggle.isChecked()) 
    {
        SharedPreferences.Editor editor = getSharedPreferences("com.example.xyz", MODE_PRIVATE).edit();
        editor.putBoolean("NameOfThingToSave", true);
        editor.commit();
    }
    else
    {
        SharedPreferences.Editor editor = getSharedPreferences("com.example.xyz", MODE_PRIVATE).edit();
        editor.putBoolean("NameOfThingToSave", false);
        editor.commit();
    }
}

To load:

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    SharedPreferences sharedPrefs = getSharedPreferences("com.example.xyle", MODE_PRIVATE);
    toggle.setChecked(sharedPrefs.getBoolean("NameOfThingToSave", true));
}

In this whatever user save to togglebtn the service must run as per that.

Use sharePreference store & retrive state of ToggleButton, based on perform action.

You can use a static boolean variable to store the state.

public class MainActivity extends Activity implements OnClickListener {

    ToggleButton tgl;
    private static boolean tglState = false;

    public static boolean getState() {
        return tglState;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tgl=(ToggleButton)findViewById(R.id.toggleButton1);
        tgl.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        if(tgl.isChecked()){
            Intent i=new Intent(MainActivity.this,Run.class);
            startService(i);
        }else{

        }
        tglState = tgl.isChecked();
    }
    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        if(tgl.isChecked()){
            tgl.setChecked(true);
        }else{

        }

        super.onBackPressed();
    }

}

And read it from other classes with

MainActivity.getState()

You can use this while your application is running.

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