Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 431
Thulio Amorim Avatar asked Jan 28 '15 18:01

Thulio Amorim


2 Answers

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();
    }
like image 62
kemicofa ghost Avatar answered Oct 21 '22 07:10

kemicofa ghost


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();
like image 35
Moubeen Farooq Khan Avatar answered Oct 21 '22 08:10

Moubeen Farooq Khan