Reputation: 725
I have toggle bottun to display the service status in the actionbar in the MainActivity
and Map
activity. After clicking a button, the user is being redirected to the map activity. The boolean serviceStauts
variable is to dectect the status of the service in both activity. I am facing problem to pass the boolean as intent to the map activity since the value is ture at the start in the MainActivity and I am getting false
in the map activity for serviceStauts
when I click the button of the checklist (I am not touching the icon in the actionbar)!?
Can guide me why I am getting false in the map activity?
MainActivity:
public class MainActivity extends ActionBarActivity {
boolean serviceStauts = true;
....
private void createCheckboxList(final ArrayList<Integer> items) {
Intent serviceStatusIntent = new Intent(MainActivity.this, Map.class);
serviceStatusIntent.putExtra("booleanServicfeStatus", serviceStauts);
startActivity(serviceStatusIntent);
}
....
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_toggle:
if (serviceStauts) {
item.setIcon(R.drawable.off);
item.setTitle("OFF");
serviceStauts = false;
mService.stopTrackingService();
System.out.println("ABC Map onOptionsitemSelected OFF");
} else {
item.setIcon(R.drawable.on);
item.setTitle("ON");
serviceStauts = true;
Intent i = new Intent(this, TrackingService.class);
startService(i);
System.out.println("ABC Map onOptionsitemSelected ON");
}
}
return super.onOptionsItemSelected(item);
}
}
Map activity:
public class Map extends ActionBarActivity {
boolean serviceStauts;
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
serviceStauts = getIntent().getExtras().getBoolean("booleanServicfeStatus");
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
System.out.println("ABC MAP onPrepareOptionsMenu was invoked.");
MenuItem item = menu.findItem(R.id.menu_toggle);
if(serviceStauts){
item.setIcon(R.drawable.on);
}else{
item.setIcon(R.drawable.off);
}
return super.onPrepareOptionsMenu(menu);
}
}
Upvotes: 1
Views: 2101
Reputation: 8211
add this to your Map
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
serviceStauts = getIntent().getExtras().getBoolean("booleanServicfeStatus");
}
You can remove your onNewIntent
method, it is used for some other cases.
Upvotes: 0
Reputation: 10288
You are using 'onNewIntentfor the first time an
Activityis called, which means the
Extras are absent. Then you use
getBooleanwhich defaults to
false` if the key doesn't exist.
You should not implement onNewIntent
except under specific circumstances. And just use getIntent
to get your bundle.
Upvotes: 1