1

I have an array which is created in activity_1, and must be used in activity_2.

How did I do this ?

I created a class (array_provider.class) which will implement the getter and setter of this array.

So in activity_1, I initialise an object of array_provider then I used it to create my array.

In activity_2, I also initialise an object of array_provider then I used it to get my array.

BUT in activity_2, I got no array (size = 0) when I'm using array_provider.getArray() even if in my activity_1 this array have a size of 7.

So I want to keep the same value of this array between activities.

I made some research before this question, and I found two options.

  1. SharedPreference => but putting an array is pretty tricky
  2. Subclass Application => not really adviced

Is there another solution ? Am I taking it wrong ?

Thanks a lot

Mxwan
  • 191
  • 13
  • Possible duplicate of [How do I pass data between Activities in Android application?](https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application) – Rodrigo Elias Feb 11 '18 at 17:26
  • This is not how you go about maintaining your data sources in an application. However, in this particular scenario you can declare that `array` static, then it will be shared between instances of your class and hence its size will not be zero in activity2. – Shoaib Anwar Feb 11 '18 at 17:46
  • And yes, please consider @Blcknx's answer. That is the way you approach saving/retrieving your data. – Shoaib Anwar Feb 11 '18 at 17:49

3 Answers3

0

you can store your array list in Shared Preference by converting in array list or you create static array that can be accessible from anywhere.

0

Reconsider the design of your application!

If you have an array list, it is most often part of a bigger model of multiple arrays (data sets). Such a model should not live in the context of a single Activity, but in the general context of the Application. All Activities have access to the common Application behind them.

Now if you create or edit such a data set, you store it within the same Activity into your model. Then you just pass on the ID of your data set by the intent. In the succeeding Activity you read your data set from the model by the given ID.

Android Studio does not create a class for the Application by default. Now many people think having a central application is not important. If it was not important, the maker of the Android libraries would not have limited intents to simple data types like IDs. They didn't design it to pass the model itself around.

Blcknx
  • 1,921
  • 1
  • 12
  • 38
0

So in activity_1, I initialise an object of array_provider then I used it to create my array.

In activity_2, I also initialise an object of array_provider then I used it to get my array.

BUT in activity_2, I got no array (size = 0) when I'm using array_provider.getArray() even if in my activity_1 this array have a size of 7.

The source of your problem:

This problem occurs because you're creating different instances of ArrayProvider and they are not the same and they don''t share the value!

Possible solutions:

  1. Make your array static. It's the easiest way but you cannot do it all the time you need to share data between activities, it's bad practice. Imagine having 20 activities , almost all your variables could end up static

  2. SharedPreferences. It may seem tricky but it's the way to go if you have some shared data among your activities.

  3. Dependency Injections. (Dagger 2) for example. It's even more complicated and not beginner-friendly but it can allow you use the same instance of a class throughout your app.

  4. Internal database. If you want to store more shared data (not just one array) you could store it in a lightweight database like Android Room for example

Conclusion, In your case of one array I would make it static as your app grows you should study other options of data sharing

Rainmaker
  • 10,294
  • 9
  • 54
  • 89