Aman Verma
Aman Verma

Reputation: 3325

SetResult() back to calling activity in DEEP LINKING?

I am implementing DeepLinking in my Android Application. Suppose I am making a payment using PhonePe and choose pay using other Application and select my Application.

Intent intent = new Intent().
intent.setData("the data");
startactivityforresult(intent, 111);

Then user selects my Application and My Splash screen then I go to other activity like this

Intent i = new Intent(Splashscreen.this, FirstActivity.class);
startactivity(i);
finish();

Then to Other Activity like this -

Intent i2 = new Intent(FirstActivity.this, SecondActivity.class);
startactivity(i2);
finish();

And After some time in SecondActivity, I send back the result to the calling activity or here the PhonePe like this -

Intent backintent = new Intent();
backintent.putExtra("somekey", "somevalue");
setResult(Result.OK, backintent);

Now when my Application closes and gets back to the Phoneme the data received by Phonepe is null.

However, if I am doing the same thing with other Application which has only one activity the data captured by the calling activity is not null and working fine.

I want to know how to send the data back to the calling activity. I am able to send the data if its only inside the same Application.

Do I have to use other tags while starting an activity?

Upvotes: 8

Views: 2110

Answers (4)

Mostafa Pirhayati
Mostafa Pirhayati

Reputation: 433

Change startActivityForResult to onActivityResult.

Upvotes: -1

Anu Martin
Anu Martin

Reputation: 731

You can only send result back to activity where the startActivityForResult called

In your case the follow like this

PhonePe Activity -> Splash Activity -> 2nd Activity -> PhonePe Activity

One StartActivityForResult is not enough for your case, because the PhonePe call your splash activity if you want to send back data you must send it from splash activity, not from 2nd activity

OR Use this trick

PhonePe Activity <-> Splash Activity <-> 2nd Activity

  1. When you get call from PhonePe start 2nd Activity for result
  2. When on 2 activity complete send the result back to Splash Activity
  3. When you get result from 2nd activity on Splash activity send result back to PhonePe

Some codes
Start SecondActivity from splash activity

Intent intent=new Intent(SplashActivity.this,SecoundActivity.class);
    intent.putExtra("SomeKey","SomeValue");
    startActivityForResult(intent,REQUEST_CODE);

Handle result on Splash Activity

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

On your second activity for send back result
if operation completed

setResult(Activity.RESULT_OK,data);


If operation failed

setResult(Activity.RESULT_CANCELED,data);



Note

2nd Activity send result back to splash Activity, and the same result send back to PhonePe from Splash Activity

Upvotes: 3

Jitesh Mohite
Jitesh Mohite

Reputation: 34210

More detailed description

We have to start activity as startActivityForResult. As name suggest you that it will return you some kind of result. This result will be come from another activity which you launch from here.

Eg : MainActivity class

   Intent intent=new Intent(MainActivity.this,SecondActivity.class);  
            startActivityForResult(intent, 2);// Activity is started with requestCode 2  

Get the result like that inside onActivityResult method.

// Call Back method  to get the Message form other Activity  
    @Override  
       protected void onActivityResult(int requestCode, int resultCode, Intent data)  
       {  
                 super.onActivityResult(requestCode, resultCode, data);  
                  // check if the request code is same as what is passed  here it is 2  
                   if(requestCode==2)  
                         {  
                            String message=data.getStringExtra("MESSAGE");   
                            textView1.setText(message);  
                         }  

SecondActivity class

Send result from this activity to previous activity which you launch.

  Intent intent=new Intent();  
                intent.putExtra("MESSAGE", "your message");  
                setResult(2,intent);  
                finish();//finishing activity  

Upvotes: 5

Anton Malyshev
Anton Malyshev

Reputation: 8861

You need to also start any new activity with startActivityForResult method then pass result back with a chain of setResult calls, getting it inside onActivityResult and setting again with setResult.

Upvotes: 8

Related Questions