Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 364
Aman Verma Avatar asked Dec 12 '17 10:12

Aman Verma


People also ask

What does registerForActivityResult return?

registerForActivityResult() takes an ActivityResultContract and an ActivityResultCallback and returns an ActivityResultLauncher which you'll use to launch the other activity. An ActivityResultContract defines the input type needed to produce a result along with the output type of the result.

How do you find the result of an activity?

You should pass the requestcode as shown below in order to identify that you got the result from the activity you started. startActivityForResult(new Intent(“YourFullyQualifiedClassName”),requestCode); In the activity you can make use of setData() to return result.

What is onActivityResult in Android?

By the help of android startActivityForResult() method, we can get result from another activity. By the help of android startActivityForResult() method, we can send information from one activity to another and vice-versa.


2 Answers

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.

like image 85
Anton Malyshev Avatar answered Sep 20 '22 20:09

Anton Malyshev


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  
like image 21
Jitesh Mohite Avatar answered Sep 21 '22 20:09

Jitesh Mohite