简体   繁体   中英

Call method from a different activity

I have a question regarding calling a method from one activity in a different activity. I have an activity which uses startActivityForResult and gets a result back after the second activity is finished, but before I call finish() on the second activity I want to call A refresh() method in the first activity.

My initial thought is to pass the first activity as an extra in the intent so I can reference the methods for the activity but can't see how this is done.

Why call refresh() on Activity A before finishing Activity B? If you have started Activity B using startActivityForResult() then you will get a result back in onActivityResult() within Activity A. You can then call refresh() at that point. So to give an example:

Activity B has:

setResult(RESULT_OK);
finish();

Activity A has:

private static final int ACTIVITY_B = 0;
...
Intent intent = new Intent(this, ActivityB.class);
startActivityForResult(intent,ACTIVITY_B );
...
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    switch (requestCode){
    case ACTIVITY_B:
        switch (resultCode){
        case RESULT_OK:
            refresh();
            break;
        }
    }
}

Or you can use other result codes to signal other events.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM