Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect that an activity has closed in Android

In my application I need to launch a SelectionActivity to select one of the options. Once the option has been selected I need to refresh another list on the MainActivity.

This is the code that I use to launch the SelectionActivity:

Intent intent = new Intent(MainActivity.this, SelectionActivity.class);
startActivity(intent);

In SelectionActivity this is the code that receives the selected option an closes the activity:

selectedValue = adapter.getItem(position);
finish();

Now the application comes back to MainActivity but I don't know how to receive an event that the SelectionActivity has closed.

Thanks

like image 958
Steven Carlborg Avatar asked Aug 26 '11 16:08

Steven Carlborg


1 Answers

Quick snippet showing use of startActivityForResult:

private static final int MY_REQUEST_CODE = 0xe110; // Or whatever number you want
// ensure it's unique compared to other activity request codes you use

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

    if (requestCode == MY_REQUEST_CODE)
        ActiviyFinishedNowDoSomethingAmazing();
}

public void onClickStartMyActivity(View view)
{
    startActivityForResult(new Intent(this, GameActivity.class), MY_REQUEST_CODE);
}

More reading on getting a result from an activity.

like image 87
noelicus Avatar answered Sep 21 '22 08:09

noelicus