startActivityForResult

A. To start another activity and do something when it’s finished:

Intent myIntent = new Intent(this, GameActivity.class);
startActivityForResult(myIntent, 1);

Then you also need to add:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//your code here to do something when it's finished
super.onActivityResult(requestCode, resultCode, data);
}

B. If you want to do something after the activity and make it dependent on the result of or data from that activity, you can set the result in the called activity as it exits, eg.

setResult(RESULT_OK) or setResult(RESULT_CANCELED)

then use it in the parent activity:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (resultCode == RESULT_OK) {
    Log.i(TAG, "Selection result ok");
    nextActivity();
  }
  if (resultCode == RESULT_CANCELED) ....
super.onActivityResult(requestCode, resultCode, data);
}

 

This entry was posted in Android. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *