0

I am trying to send data from activity to other via intent. Sending String or integer work perfectly fine. But when I add Parecelable object in same intent ,it always return null for Strings , Integers and Object. Below is my code for setting data to intent.

Intent intent = new Intent(SplashActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("SomeText", "sometext");
intent.putExtra("itemId", itemId);
intent.putExtra("someuser",user);

startActivity(intent);

And in other activity I am getting values as:

String som = getIntent().getStringExtra("SomeText");
int itemird = getIntent().getIntExtra("itemId", 0);
User user = getIntent().getParcelableExtra("someuser");

I also have tried different other solutions as this and this and this

Can anybody here suggest me what my mistake is or any better solution?

So this is my Parcelable class

public class User implements Parcelable{
    private int mData;
    private String username;
    private String imagePath;
    private String userId;

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setImagePath(String imagePath) {
        this.imagePath = imagePath;
    }

    public String getImagePath() {
        return imagePath;

    }

    public User() {

    }

    public User(String username, String userId) {
        this.username = username;
        this.userId = userId;
    }
    public User(String username) {
        this.username = username;
    }
    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {

    }

    public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>() {
        public User createFromParcel(Parcel in) {
            return new User(in);
        }

        public User[] newArray(int size) {
            return new User[size];
        }
    };

    private User(Parcel in) {
        mData = in.readInt();
    }

}
Community
  • 1
  • 1
Saira Nawaz
  • 710
  • 2
  • 10
  • 24
  • You can make your Custom Java Object implement Serializable and use putExtra. If it didn't work , paste error logs. – Aashish Aadarsh May 16 '17 at 08:05
  • It's probably an issue with the implementation of the parcelable interface methods in your User class. Can you post these methods here? – aravindsagar May 16 '17 at 08:06
  • you must persist all property in `writeToParcel` and read all of them in constructor `User(Parcel in)` see this link to get better my mean http://stackoverflow.com/questions/21250339/how-to-pass-arraylistcustomeobject-from-one-activity-to-another/21250450#21250450 – Shayan Pourvatan May 16 '17 at 09:54

7 Answers7

0

Carefully read this answer of Jeremy Logan. You should:

1) implement Parcelable for Your User class;

2) class cast for Your User class like User user = (User) getIntent().getParcelableExtra("someuser") and intent.putExtra("someuser", (Parcelable)user).

Community
  • 1
  • 1
Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
0

Pass object like this structure

Bundle b = new Bundle();
b.putParcelable("someuser",user);
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("SomeText", "sometext");
intent.putExtra("itemId", itemId);
intent.putExtra(b);
startActivity(intent);

then received like this

Bundle b = getIntent().getExtras();
if(b != null) {
User user = b.getParcelable("someuser");   
}
sasikumar
  • 12,540
  • 3
  • 28
  • 48
0

Create a Parcelable class

    public class User implements Parcelable{

        String name;


        @Override
        public int describeContents() {
            // TODO Auto-generated method stub
            return 0;
        }

        /**
        * Storing the User data to Parcel object
        **/
        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(name);


        }

        /**
        * A constructor that initializes the User object
        **/
        public User(String uName){
            this.name = uName;

        }

        /**
        * Retrieving User data from Parcel object
        * This constructor is invoked by the method createFromParcel(Parcel source) of
        * the object CREATOR
        **/
        private Student(Parcel in){
            this.mSName = in.readString();

        }

        public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>() {

            @Override
            public User createFromParcel(Parcel source) {
                return new User(source);
            }

            @Override
            public User[] newArray(int size) {
                return new User[size];
            }
        };
    }

public class MainActivity extends Activity {

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

       mBtnOk.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    // Creating an instance of Student class with user input data
                    User userObj= new User ("Name");

                    // Creating an intent to open the activity 
                    Intent intent = new Intent(MainActivity.this, NextActivity.class);

                    // Passing data as a parecelable object to 
                    intent.putExtra("user",userObj);

                    // Opening the activity
                    startActivity(intent);
                }
            });



    }

    }





    public class NextActivity extends Activity {

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_next);

     // Fetching data from a parcelable object passed from MainActivity
            User user= getIntent().getParcelableExtra("user");



    }

    }
Atif AbbAsi
  • 5,633
  • 7
  • 26
  • 47
  • @SairaNawaz check your class in your writeToParcel() method dest.writeString(name); add this line in your code for name and id.! – Atif AbbAsi May 16 '17 at 09:26
0

Try this solution with change your User class like below ans it works:

public class User implements Parcelable {
private int mData;
private String username;
private String imagePath;
private String userId;

public String getUserId() {
    return userId;
}

public void setUserId(String userId) {
    this.userId = userId;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public void setImagePath(String imagePath) {
    this.imagePath = imagePath;
}

public String getImagePath() {
    return imagePath;

}

public User() {
}

public User(String username, String userId) {
    this.username = username;
    this.userId = userId;
}
public User(String username) {
    this.username = username;
}
@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {

    dest.writeInt(mData);
    dest.writeString(username);
    dest.writeString(imagePath);
    dest.writeString(userId);
}

private void readFromParcel(Parcel in ) {

    mData     = in .readInt();
    username  = in .readString();
    imagePath = in .readString();
    userId    = in .readString();
}

public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>() {
    public User createFromParcel(Parcel in) {
        return new User(in);
    }

    public User[] newArray(int size) {
        return new User[size];
    }
};

    private User(Parcel in) {
        readFromParcel( in );
    }

}
Nitin Patel
  • 1,605
  • 13
  • 31
0

I think you didn't implement your user model class properly. You can try this

public class User implements Parcelable {

private int mData;
private String username;
private String imagePath;
private String userId;

public User(String username, String userId) {
    this.username = username;
    this.userId = userId;
}
public User(String username) {
    this.username = username;
}
public User() {} // empty constructor

public String getUserId() {
    return userId;
}

public void setUserId(String userId) {
    this.userId = userId;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public void setImagePath(String imagePath) {
    this.imagePath = imagePath;
}

public String getImagePath() {
    return imagePath;

}

@Override
public int describeContents() {
    return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {

    dest.writeInt(mData);
    dest.writeString(username);
    dest.writeString(imagePath);
    dest.writeString(userId);
}

private User(Parcel in) {
    mData     = in.readInt();
    username  = in.readString();
    imagePath = in.readString();
    userId    = in.readString();.
}

public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>() {
    public User createFromParcel(Parcel in) {
        return new User(in);
    }

    public User[] newArray(int size) {
        return new User[size];
    }
};

}

Then in your Splash activity, you can do this. I'm assuming you have created a user object to want to send to MainActivity

Intent intent = new Intent(SplashActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("someuser",user); // using the (String name, Parcelable value) overload! 
intent.putExtra("SomeText", "sometext");
intent.putExtra("itemId", itemId);
intent.putExtra(bundle);

startActivity(intent);

You can then receive it in MainActivity like this

User user = (User) getIntent().getParcelableExtra("someuser");
0

Hi why you use parcelable if serializable is easy to use and easy to understand and implement

implement serializable in your class

public class Place implements Serializable {
        private int id;
        private String name;

        public void setId(int id) {
            this.id = id;
        }

        public int getId() {
            return id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    } 

Then you can pass this object in intent

     Intent intent = new Intent(this, SecondAct.class);
     intent.putExtra("PLACE", Place);
     startActivity();

in the second activity you can get data like this

 Place place= (Place) getIntent().getSerializableExtra("PLACE");

parcelable is used when there is large data need to send other wise you can use serializable

Let me know if not work

Sushant Gosavi
  • 3,647
  • 3
  • 35
  • 55
0

I tried all solutions but no one mentioned this solution, the writeToParcel() is problematic. The parcel should be written and read in the same order.

Hagar Magdy
  • 293
  • 2
  • 10