Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip one activity while sending back data using intents?

Consider I've 3 activities Activity1 Activity2 Activity3 if I want to send back data to Activity1 from Activity3 skipping Activity2 what should I do?

like image 648
Vaibhav Kadam Avatar asked Dec 11 '22 15:12

Vaibhav Kadam


2 Answers

There is a "correct" way to this adding FLAG_ACTIVITY_FORWARD_RESULT to the intent.
Which is used to start the next activity and notify it to pass the result to the first one:

Code:

Activity 1 -> startActivityForResult(activityB,0);
Activity 2 -> activityCintent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
              startActivity(activityCintent); finish();
Activity 3 -> setresult(10); finish();
Activity 1 -> if(result==10) {dofunction(); } 
like image 61
Nir Duan Avatar answered May 14 '23 19:05

Nir Duan


Restarting of activity1 again is not a good way.

You can achieve this using "startActivityForResult"

Use below code, to start Activity2

Intent intent = new Intent(mContext, Activity2.class);
startActivityForResult(intent, REQUEST_CODE);

Use below code, to start Activity3

Intent intent = new Intent(mContext, Activity3.class); startActivityForResult(intent, REQUEST_CODE);

In activity3 use the below code where you want to send the data to activity1:

Intent intent = getIntent();
        intent.putExtra("Key", value);
        setResult(RESULT_OK, intent);
        finish();

Override onActivityResult in Activity1 and Activity2.

In onActivityResult of activity2,check the request code and Result code. Based on this call finish(), and set the values as below :

 boolean value = data.getBooleanExtra("key, false);

 Intent intent = getIntent();
            intent.putExtra("Key", Value);
            setResult(RESULT_OK, intent);
            finish();

In onActivityResult of activity1 you will get the data.

like image 42
madhuri H R Avatar answered May 14 '23 20:05

madhuri H R