1

So the case is simple, I have RegisterActivity, and I want to create an arraylist of class user and store the data about id, email, and password. Then, I want to pass that value to the LoginActivity so I can use it to validate if the user data was already created or not.

But, everytime I pass the arraylist value, it works for the first time, and then after I comeback to RegisterActivity and try to register again, it doesn't store to the 2nd value of the arraylist user, but store to the 1st value, which is the arraylist is empty when I comeback to the RegisterActivity.

This is what i do.

In the RegisterActivity

                users.add(new User(newId,strEmail,strPass));

                Intent i = new Intent(RegisterActivity.this,LoginActivity.class);

                Bundle bundle  = new Bundle();
                bundle.putSerializable("userData",users);
                i.putExtras(bundle);
                startActivity(i);

In the LoginActivity

ArrayList<User> users;
Bundle bundleUser;

    bundleUser = getIntent().getExtras();
    users = (ArrayList<User>) bundleUser.getSerializable("userData");

Do you have any solution?

Please help me

SandPiper
  • 2,816
  • 5
  • 30
  • 52
  • 1
    I think your data lost during activity finish state, so you should keep your data in global state...you can use any other singleton class or keep your date into Application class... – Amit Oct 21 '18 at 12:24
  • 2
    You can also store your data in SharedPreference or SQLite DB – Sniffer Oct 21 '18 at 12:25
  • as Sniffer mentioned, u can use shared preferences easily for this task. Navigate to answer of this https://stackoverflow.com/questions/7057845/save-arraylist-to-sharedpreferences . It contains way to serialize arraylist and add to shared perferences – PushpikaWan Oct 21 '18 at 12:42

1 Answers1

0

Armit and Sniffer are both correct (I didn't have time to check lucefer's link on serializing the entire array and storing it at one time). You have multiple options here depending on your situation.

As a general "rule of thumb" when storing values in Android:

When you want to temporarily store data while the user has the app open (even when the app is moved to the background), but want the data to reset to original values (0, false, or null if not defined) every time you close your application:

-Utilize SavedInstanceState which will even protect data from screen rotation changes.

OR

-Create a new Java Class and store your data there. Data stored in a Java class file will keep its value as long as the app is open (even when the app is moved to the background), where as data stored in an Activity will reset its values on orientation changes or even when moving to another activity if the data is not being explicitly stored or passed... (BELOW IS AN EXAMPLE OF A SINGELTON JAVA CLASS)

public class RegularJavaClass {
   public static RegularJavaClass myObj;

   public RegularJavaClass(){}

   public static RegularJavaClass getInstance() {
    if (myObj == null) {
        myObj = new RegularJavaClass();
    }

    return myObj;
   }

    private int myNumValue;
    private String myStringValue;

    public int getMyNumValue() {
    return myNumValue;
    }
    public void setMyNumValue(int myNumValue) {
    this.myNumValue = myNumValue;
   }
    public String getMyStringValue() {
    return myStringValue;
    }
    public void setMyStringValue(String myStringValue) {
    this.myStringValue = myStringValue;
    }
   }

THEN WHEN YOU CALL THIS CLASS TO STORE STUFF USE

int someNum = 3000;
String someString = "saveThisString";

RegularJavaClass.getInstance().setMyNumValue(someNum);
RegularJavaClass.getInstance().setMyStringValue(someString);

THEN WHEN YOU WANT TO RETRIEVE THIS DATA

int retrieveNum = RegularJavaClass.getInstance().getMyNumValue();
String retrieveString = RegularJavaClass.getInstance().getMyStringValue();

When you want to store a small amount of data and have it be retained even when the application is closed and opened again at a later time (or until the application has been un-installed or explicitly changed by the developer)

-Use sharedPreferences. Think of it as a tiny database that every android application comes with automatically. Remember only store SMALL amounts of data here

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                SharedPreferences prefs =
                        getSharedPreferences("StorageName", MODE_PRIVATE);

                SharedPreferences.Editor editor = prefs.edit();
                //The value type determines the "put" in your case break the
                //array into a for statement and save each value
                for (String s : myArray) {
                    //s would be the value
                    //make a key for your stored value just like a HashMap
                    editor.putString(key, value).apply();

                }

            }
        });
        t.start();
    }
 }

THEN TO RETRIEVE YOUR STORED VALUES LATER

   @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

        SharedPreferences prefs =
                getSharedPreferences("StorageName", MODE_PRIVATE);
                //Always enter a default value for just in case the storage 
                 //value is empty
                String myStoredValue = prefs.getString(key,null);
    }
}

When you want to store a LARGE amount of data and have it be retained even when the application is closed and opened again at a later time (or until the application has been un-installed or explicitly changed by the developer)

-Use a database such as SQLite

Paris B. G.
  • 374
  • 1
  • 3
  • 14
  • hi, thanks before you taught many new things for me. Ok, so i'm already tried your first solution using the new singleton java class, and then i made an arraylist of class User named user there and then make a method called saveUserdata which action is to add the data to the arraylist. and i use that method on the RegisterActivity. And then after i access the arraylist in the LoginActivity, and i check the size of the array, the arraylist stated 0 value. do you know why is that? – Kelvin Fernandez Oct 21 '18 at 13:34
  • yea you probably didn't make an actual Singleton. By Singleton, it basically means that the class is static (only one reference can be made of the class object) Make sure you use the same class object to save the variables as you do to retrieve the variables – Paris B. G. Oct 21 '18 at 14:58
  • I'll update my answer to a true Singleton without having to use the "static" keyword everywhere because that can get messy and cause leakage if you aren't careful – Paris B. G. Oct 21 '18 at 15:01
  • If this doesn't work for some reason I'll check back again later, if it DOES solve your problem, please "Accept my answer" by clicking the green checkmark" on the left side of my answer : -) Good Luck! – Paris B. G. Oct 21 '18 at 15:15
  • Thanks a lot man, you saved my college task, it works fine but i still didn't know what does singleton class means, maybe i should learn more about that – Kelvin Fernandez Oct 21 '18 at 16:15