1

I'm trying use a value retrieved from Activity3 to Activity1. This is my flow:

Activity1 calls Activity2 → Activity2.finish(), call Activity3 → Activity3.doSomethingAndGetValue() → Activity3.finish() → Activity1.useDoSomethingAndGetValue();

I can not find the way to send back the values from Activity3 to Activity1; Even using startActivityForResult() because Activity2 was already destroyed by that time.

I tried to use a serializable interface and pass this as extra value on the intent, but this solution only works with the object passed is a static which doesn't allow me to do any change in the current member variables on Activity 1.

Besides, I've tried all these options without success:

https://stackoverflow.com/a/17306696/3217203

(I'm not allow to use shared preferences for this)

Does anyone has any idea?

Note: I can't add eventBus, I know would be great and way easier but the architecture didn't allow me.

Community
  • 1
  • 1
Mauricio Sartori
  • 2,533
  • 6
  • 26
  • 28

4 Answers4

5

You might like to make use of the intent flag FLAG_ACTIVITY_FORWARD_RESULT as described in Intent when starting activities 2 and 3.

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

this is the logic of FLAG_ACTIVITY_FORWARD_RESULT. First you start an activity for result, new activity sends this intent to another new activity until you set result.

Nir Duan
  • 6,164
  • 4
  • 24
  • 38
0

If you the data requirement is limited to the three activities as in your description, I would suggest, not finish activity2 before going to activity3. Instead, make use of startActivityForResult() the activities. When returning from activity3 to activity2, call finish() and then return to activity1.

If the above solution is not acceptable, try the following 2 approaches:

(1) You could use variables with an application context. For this, first create an Application class say MyApp.java,

public class MyApp extends Application {

private String data;

public String getData() {
       return data;
   }

   public void setData(String data) {
        this.data = data;
   }
}

Now, add this class as android:name for the application tag in manifest,

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tony.myapp">

<application
    android:name=".MyApp"
    .....>

In the Activity3,

String data = "I am BATMAN!!";
MyApp myApp = (MyApp) getApplicationContext();
myApp.setData(data);

Then, all you need to do in the Activity1 is,

MyApp myApp = (MyApp) getApplicationContext();
String data = myApp.getData();

(2) If you have simple key-value pairs that you want to persist different instances of the app, use SharedPreferences.

Tony
  • 2,242
  • 22
  • 33
0

U can Use setter and getter pojo class

Setvalues from activity3 and use them in activity 1.

public class MyPojo   {
   private static String value;
   public static String   getValue() {
    return value; 
       }
     public static void setValue(String value) 
     { 
           MyPojo.value = value; 
      }
 } 

In the Activity3,

MyPojo.setValue("this is activity3");

Then in the Activity1 ,

MyPojo.getValue();

Preeti Rani
  • 685
  • 7
  • 17
0

As Tony suggested you can use the App class (which is a singleton), but I would create another singleton class, specific for your purpose. Like this...

public class Singleton {
    private static Singleton INSTANCE = null;
    private String aValue;
    private int bValue;
    private Object cValue;

    private Singleton(){

    }

    public static Singleton getInstance(){     
        if(INSTANCE == null){
            INSTANCE = new Singleton();
        }
        return INSTANCE;
    }
    //Setters and getters here
}

In that way you can get the same instance (by using the static method Singleton.getInstance()) in all your activities and change/read its values as you need.