Blaze Tama
Blaze Tama

Reputation: 10948

Getting intent result from ondestroy

I can get the result from intent using onBackpressed :

returnIntent.putExtra("isBack", true);
setResult(RESULT_OK,returnIntent);     
finish();

And this is my onActivityResult :

if (requestCode == 1) 
        {
             if(resultCode == RESULT_OK){      
                 boolean isBack = data.getBooleanExtra("isBack", false);
                 if(isBack == true)
                     searchEditText.setText("");
             }
         }

However, now i need to do the setResult not in the onBackpressed, so i was thinking of onDestroy.

I tried to move the setResult to onDestroy but the onActivityResult cant get my result.

I also have tried onStop and onPause, but none of them are working.

Please help me, Thanks for your help

Upvotes: 0

Views: 1913

Answers (3)

Sushil
Sushil

Reputation: 8478

It is not mandatory to put setResult() just before exiting the activity. You can put it anywhere before after your required action is accomplished in the activity. It will be called properly after your activity exit.

Normally when one activity is called from another (or parent activity comes up when child is finished), the destruction process of one activity and construction process of another activity happen in parallel. It is not like first the activity is completely destroyed and then another activity is created. So the process happen in parallel and asynchronously. So you should not do it in onStop(), onDestory() and similar methods.

Upvotes: 1

Stan Smith
Stan Smith

Reputation: 906

Override in finish()

@Override
public void finish() {
    Intent data = new Intent();
    data.putExtra("isBack", true);
    setResult(RESULT_OK, data); 

    super.finish();
}

Upvotes: 7

Cloudream
Cloudream

Reputation: 651

What are you trying to do?

You could call setResult() as soon as the action finished, no need to wait until activity finish/destroy.

Upvotes: 2

Related Questions