Vaibhav Kadam Vaibhav Kadam - 1 year ago 80
Android Question

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?

Answer Source

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);

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);

In onActivityResult of activity1 you will get the data.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download