Android - Passing an Object To Another Activity

后端 未结 3 1838
逝去的感伤
逝去的感伤 2021-01-27 06:49

I am utilizing the follow class, which I have as an object: http://pastebin.com/rKmtbDgF

And I am trying to pass it across using:

Intent booklist = new I         


        
相关标签:
3条回答
  • 2021-01-27 07:03

    go here, and paste your class structure.it will create parcelable class for you and then you can easily pass your object around activities.

    0 讨论(0)
  • 2021-01-27 07:13

    Easiest Way to do this is to implement Serializeable ..

    import java.io.Serializable;
    
    @SuppressWarnings("serial") 
    public class Student implements Serializable {
    
    public Student(int age, String name){
        this.age = age;
        this.name = name;
    }
    
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
    private int age;
    private String name;
    
    }
    

    Sending object from Activity A to Activity B

    Student student = new Student (18,"Zar E Ahmer");
    Intent i = new Intent(this, B.class);
    i.putExtra("studentObject", student);
    startActivity(i);
    

    Getting Object in Activity B.

    Intent i = getIntent();
    Student student = (Student)i.getSerializableExtra("studentObject");
    
    0 讨论(0)
  • 2021-01-27 07:30

    Try the following post.

    How can I make my custom objects Parcelable?

    The problem is that the ImageManager is not a parable Object. So that's giving the cast error. If the imageManager is yours you should make the class implement Pracabale like the url above sais.

    EDIT:

    Above should be the right way of doing Parceables, But in your case i really would say you shouldn't pass the ImageManager between different activities. Because the context that your using in the ImageManager class will be disposed.. And you'll certainly get an error.

    So why don't you make a new instance of the class instead and only pass the Bitmap in to it (After transferring the bitmap and other not context related information with the bundle off course.)

    0 讨论(0)
提交回复
热议问题