Mr Asker
Mr Asker

Reputation: 2380

Send boolean intent from onBackPressed() to onStart()

I am trying to send an intent from the Map activity to the MainActivity when I press the back button in the Map activity. The intent should be received in the onStart() of the MainActivity but I am just getting for extras NULL there. The variable serviceStatus in the Map activity is set when the user clicks the button in the actionbar. I want to set the status of the service in the actionbar of the MainActivity.

I have tried to send the intent from the finish() in the Map activity but I am getting null for extras too.

How can I fix it?

Map activity:

public class Map extends ActionBarActivity{
 boolean serviceStatus;

    @Override
    public void onBackPressed() {
        super.onBackPressed();      
        Intent serviceStatusIntent = new Intent(Map.this, MainActivity.class);
         // I have debugged it and serviceStatus is false here also the intent exists.
        serviceStatusIntent.putExtra("ServiceStatusMapExtras", serviceStatus);
        startActivity(serviceStatusIntent);
    }

MainActivity:

public class MainActivity extends ActionBarActivity {
    @Override
boolean serviceStatus = true;
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();                
        Bundle extras  = getIntent().getExtras();
        //I am receiving null here.     
        if(extras != null){
            serviceStatus = extras.getBoolean("ServiceStatusMapExtras");

        }

Upvotes: 1

Views: 373

Answers (1)

dieter_h
dieter_h

Reputation: 2727

Remove super.onBackPressed();

Fix MainActivity:

public class MainActivity extends ActionBarActivity {
        @Override
      boolean serviceStatus = true;
        protected void onStart() {
            super.onStart();                
            serviceStatus =  getIntent().getBooleanExtra("ServiceStatusMapExtras", false);

       }
}

Upvotes: 1

Related Questions