I have the following scenario: I have a model class, which looks like this:
public class UserModel implements Serializable {
private String userEmail, userName;
public UserModel() {}
public UserModel(String userEmail, String userName) { this.userEmail = userEmail; this.userName = userName;}
//setters and getters
}
In my first activity, I'm logging in to Firebase, I'm getting the data from the FirebaseUser
object and then I'm creating an object of the UserModel
class and passing it to intent like this:
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("userModel", userModel);
startActivity(intent);
In the second activity, every time it starts, I'm checking if the user is logged in or not. If it is, it stays here, if not I redirect to the first activity. Here, I'm getting the object using the following line of code:
UserModel userModel = (UserModel) getIntent().getSerializableExtra("userModel");
First time when the activity starts, everything works fine, but when I restart the activity, I get a NullPointerException
. How can I preserve the userModel
object that I got from the intent through any activity restarts?
Thanks in advance!