0

I'm trying to pass dataUser object from Register activity to Login activity. But dataUser in Login activity is null.

My DataUser class implements Serializable

RegisterActivity.java:

DataUser dataUser = new DataUser(null, null,null);

dataUser.setName(nameET.getText().toString());

dataUser.setSurname(surnameET.getText().toString());

dataUser.setEmail(emailET.getText().toString());

Bundle bundleID = new Bundle();
bundleID.putSerializable("user", dataUser);
Intent intent = new Intent(getApplicationContext(),LoginActivity.class);
intent.putExtra("bundle", bundleID);
startActivity(intent);

LoginActivity.java:

 Bundle objectUser = getIntent().getExtras();
 if (objectUser!=null){
        dataUser= (DataUser) objectUser.getSerializable("user");
        emailET.setText(dataUser.getEmail());
 }

When I'm debugging this is what I get in Register Activity: CodeRegisterActivity

dataUser and bundle aren't empty

And this is the result in LoginActivity:

login getSerializable

dataUser is null

I was trying to find the solution in other similar question, but I didn't find it. Any ideas for solving this?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Raquel
  • 23
  • 6
  • 2
    see https://stackoverflow.com/questions/2139134/how-to-send-an-object-from-one-android-activity-to-another-using-intents – TWL Aug 24 '18 at 16:42

3 Answers3

1

When you call get getIntent().getExtras(); you are getting a bundle that contains a bundle that contains your DataUser.

So you could either put the data user directly in the extras intent.putExtra("user" , (Serializable) dataUser);

Or get the user out of the bundle

Bundle extras = getIntent().getExtras();
Bundle bundle = extras.get("bundle");
dataUser= (DataUser) bundle.getSerializable("user");
Zachary Sweigart
  • 1,051
  • 9
  • 23
0

Pass your list in intent.putExtra(), for example

Intent intent = new Intent(getApplicationContext() , LoginActivity.class);
intent.putExtra("user" , (Serializable) dataUser );
startActivity(intent);

and for retrieve list in NextActivity

Intent intent = getIntent();
dataUser= (DataUser)intent.getSerializableExtra("user");
Nisarg
  • 172
  • 6
  • That's it! Simple as that. Thank you so much I don't know why I was obsessed with doing with de Bundle – Raquel Aug 24 '18 at 16:53
  • If you find it helpful than marked it as right.Happy coding @Raquel – Nisarg Aug 24 '18 at 16:55
  • I'm new here and I think I can only vote for one, if you know how can I vote for more than one let me know! – Raquel Aug 24 '18 at 17:32
0

That is because you use two bundles, i mean you put in intent bundle and you have your object bundle.

try this

Intent intent = new Intent(getApplicationContext(),LoginActivity.class);
intent.putExtra("user",dataUser);
startActivity(intent);

And in your second activity try this:

 Bundle objectUser = getIntent().getExtras();
 if (objectUser!=null){
        dataUser= (DataUser) objectUser.getSerializable("user");
        emailET.setText(dataUser.getEmail());
 }
Daniel Carreto
  • 211
  • 1
  • 9