0

So I'm having 4 Activities. Let's call them Activity1, Activity2, Activity3 and Activity 4.

Now what i want to do is: I enter data as an array in Activity2 and want to use it in Activity4. Now the solution that i found says, that i can just use intent.putExtra, and then startActivity(intent).

But my problem is: I don't want to start Activity 4 after Activity2. For Example: i want to be able to enter the data in Activity2 then go to Activity3 or 1 and afterwards go to activity4 and be able to use the data from Activity2. So is there another way i can pass the data to activity4 without being forced to start it? Im talking about something like for example "sessionStorage.set("",)" in Javascript/JQuery.

I hope my question isn't too confusing and thank you in advance.

Kasım Özdemir
  • 5,414
  • 3
  • 18
  • 35

3 Answers3

2

If you don't want to start the Activity4. then you have 2 options to get the data in Activity4 without start to it. Saved the data into shared preferences or into the database, where you enter the data in Activity.

For saving the array data into shared preference, check this link. Save ArrayList to SharedPreferences

mdroid
  • 474
  • 3
  • 15
2

Another option might be this. You can create a singleton class. You can access this class from any activities.

public class Globals {
    private static Globals instance;

    private String [] strings = null;

    private Globals() {
    }

    public static synchronized Globals getInstance() {
        if (instance == null) {
            instance = new Globals();
        }
        return instance;

    }


    public void clear(){
        strings = null;
    }

    public String[] getStrings() {
        return strings;
    }

    public void setStrings(String[] strings) {
        this.strings = strings;
    }
}

Activity:

String [] strings = new String[]{"item1","item2"};
Globals.getInstance().setStrings(strings);

Other Activity:

String [] strings = Globals.getInstance().getStrings();

All data is delete when the application closed.

Kasım Özdemir
  • 5,414
  • 3
  • 18
  • 35
1

You can store the data entered into SharedPreferences in Activity2 and retrieve it in Activity4.
Alternatively, you can send the data to Activity3 or Activity1 and then afterwards send the data to Activity4.

Ismail Shaikh
  • 482
  • 4
  • 13