aletede91
aletede91

Reputation: 1166

onActivityResult is not call in my app

I have two activities "A" and "B".

In my "A" activity I use startActivityForResult:

Intent i = new Intent(A.this, B.class);
setResult(RESULT_OK, i);
startActivityForResult(i, 121245);
finish();

This is the code of my "B" activity:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {

    Toast.makeText(B.this, "onActivityResult", Toast.LENGTH_SHORT).show();          

    if(requestCode == 121245) {

        if (resultCode == RESULT_OK) 
            //make something        
    }
}

Why my Toast doesn't show?

Upvotes: 0

Views: 1121

Answers (3)

Pasi Matalamäki
Pasi Matalamäki

Reputation: 1853

onActivityResult will be called on the activity which is starting the activity for result, meaning calls the startActivityForResult method

What that means is if you want to be notified when the B activity finishes in A activity, you would first in A activity start the B activity like you did in your example code

Intent i = new Intent(A.this, B.class);
setResult(RESULT_OK, i);
startActivityForResult(i, 121245);

then when B activity finishes A activity's onActivityResultis called and there you can do whatever you want.

Here's a diagram if it helps you understand the flow of the application

diagram

Upvotes: 9

Piotr Golinski
Piotr Golinski

Reputation: 1000

You have a little mistake

at A class should be:

Intent i = new Intent(A.this, B.class);
startActivityForResult(i, 121245);

at B class to return:

Intent i = new Intent();
setResult(RESULT_OK, i);
finish();

and handle it at A

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {

    Toast.makeText(B.this, "onActivityResult", Toast.LENGTH_SHORT).show();          

    if(requestCode == 121245) {

        if (resultCode == RESULT_OK) 
            //make something        
    }
}

Upvotes: 5

Tayyab Hussain
Tayyab Hussain

Reputation: 1838

onActivityResult() 

must be the part of your A activity, Your B activity will return some data back to A and then onActivityResult() will be called.

Upvotes: 1

Related Questions