0

I am trying to open another activity by intent and putExtra. It used to run fine previously, but now it crashes.

startActivity(new Intent(ForgotPassword.this, OtpVerification.class)
                                .putExtra("user", user)
                                .putExtra("otp", verificationId));
                        finish();

the user here is a class object and am receiving it in another activity

user = getIntent().getParcelableExtra("user");

the app is not even going to another activty and crashes without any error message in logcat. It only happens if i add .putExtra code

Narendra_Nath
  • 4,578
  • 3
  • 13
  • 31
Ankit Verma
  • 496
  • 5
  • 21

3 Answers3

0

Try adding the putExtra in the following way

Intent intent = new Intent(ForgotPassword.this, OtpVerification.class);
intent.putExtra("user", user);
intent.putExtra("otp", verificationId);
startActivity(intent);
finish();

and then get the intent extras

Intent intent= getIntent();
Bundle extras = intent.getExtras();
if(extras != null)
    String data = extras.getString("keyName");
lyncx
  • 680
  • 4
  • 8
0

Your user is an Object that you're trying to pass from one activity to another. So you can create a custom class that implements the Serializable interface.(you must be able to use Parcelable too but i don't have experience with it )

//To pass: intent.putExtra("User", user);

// To retrieve object in second Activity getIntent().getSerializableExtra("User");

Point to note: each nested class in your main custom class must have implemented the Serializable interface eg:

class MainClass implements Serializable {

    public MainClass() {}

    public static class ChildClass implements Serializable {

        public ChildClass() {}
    }
}

Ref:How to pass an object from one activity to another on Android

Narendra_Nath
  • 4,578
  • 3
  • 13
  • 31
0

Thank you for your suggestions, the actual problem I found was that my user object had a variable called image (String type) and had a value of length about 1,000,000 (I was storing string encoded image using base64 in it). Once i decreased the size to thousands , it worked. I don't know why the app cannot send such large data across activities.

Ankit Verma
  • 496
  • 5
  • 21