Thulio Amorim
Thulio Amorim

Reputation: 191

Make an activity A wait an activity B finish to continue

I have an activity A with this fuction:

public void onSettingsClick(View view){
        Intent intent = new Intent(this, Settings.class);
        startActivity(intent);

        checkSettings();
}

Is there any way to make the activity A to wait for the activity B ("Settings.class") finish to execute the fuction "checkSettings();" ?

Thanks

Upvotes: 9

Views: 9826

Answers (3)

Christopher Myers
Christopher Myers

Reputation: 107

Another approach to doing this since you are launching a separate activity is call check settings in onResume. This way since you're not having to pass any data and if the are other reasons you may want to refresh or recheck your settings, it will do so any time the activity is brought back to the top.

    override fun onResume() {
        super.onResume()
        checkSettings()
    }

Upvotes: 0

kockburn
kockburn

Reputation: 17606

Edit: I may have misunderstood your question. If you want to run checkSettings() function in B then you need to define and call that function in your B activity.

Otherwise, if you want to wait for activity B to end before running checkSettings() then copy the following code.

In A:

public void onSettingsClick(View view){
        Intent intent = new Intent(this, Settings.class);
        startActivityForResult(intent, 1);
}

then also in A Override onActivityResult method.. this gets called when B ends:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        checkSettings();
    }

Upvotes: 9

Moubeen Farooq Khan
Moubeen Farooq Khan

Reputation: 2885

In your Activity A write

public void onSettingsClick(View view){
    Intent intent = new Intent(this, Settings.class);
    startActivityForResult(intent, 100);
}

and also in you Activity A override onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

   if(resultCode==RESULT_OK){

      if(requestCode==100){

        checkSettings();

       }
   }

}

and in your Activity B

when you want to finish that activity write that piece of code there

Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);
finish();

Upvotes: 5

Related Questions