Reputation: 33
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();
}
}
Upvotes: 0
Views: 365
Reputation: 1019
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.
Upvotes: 0
Reputation: 2401
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));
}
Upvotes: 1
Reputation: 10969
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.
Upvotes: 0