Parcelable object passing from android activity to fragment

帅比萌擦擦* 提交于 2021-02-08 11:24:45

问题


I'm trying to pass an object that implements Parcelable from an activity to a fragment. I know how to pass from activity to activity. I just want to try this. But when I received the object it always receivednull. How can I resolve this problem?

currentObject is the object instance of the class which implements Parcelable

ContentMainFragment is the Fragment class

In the activity

   Fragment fragment = new ContentMainFragment();
   Bundle bundle = new Bundle();
   bundle.putParcelable("SampleObject", currentObject);
   fragment.setArguments(bundle);

In the fragment

Bundle bundle = this.getArguments();
if (bundle != null) {
            currentObject = bundle.getParcelable("SampleObject");
link = currentObject.getLink();
}

Thank you.


回答1:


Try this

First, change some small things.

MainActivity.java

// 1. Parse the object to the fragment as a bundle;
ContentMainFragment contentMainFragment = new ContentMainFragment();

Bundle bundle = new Bundle();
bundle.putParcelable("SampleObject", currentObject);
contentMainFragment.setArguments(bundle);

// 2. Commit the fragment.
getSupportFragmentManager().beginTransaction()
        .add(R.id.fragment_container, contentMainFragment).commit();

ContentMainFragment.java

// 1. Get the object in onCreate();
if (getArguments() != null) { 
    link = getArguments().getParcelable("SampleObject").getLink(); 
}

Second, it doesn't seem there is something wrong with your approach, then double check if you are parsing a valid object (currentObject) in the Activity.




回答2:


I show you some code to help you:

Model

public class Model implements Parcelable {
    private String name;

    public Model(){

    }

    public String getName() {
        return name;
    }

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

    protected Model(Parcel in) {
        name = in.readString();
    }

    public static final Creator<Model> CREATOR = new Creator<Model>() {
        @Override
        public Model createFromParcel(Parcel in) {
            return new Model(in);
        }

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

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(name);
    }
}

MainActivity

Model currentObject = new Model();
        currentObject.setName("Testing arguments");

        TestFragment fragment = new TestFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable("SampleObject", currentObject);
        fragment.setArguments(bundle);

TestFragment

public class TestFragment extends Fragment {

    Model model;

    public TestFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = this.getArguments();
        if (bundle != null) {
            model = bundle.getParcelable("SampleObject");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
         View v = inflater.inflate(R.layout.fragment_test, container, false);
         TextView tvArgument = v.findViewById(R.id.tvTestArgument);
         tvArgument.setText(model.getName());
         return v;
    }
}

This code works properly.

But normally in my case when I have to pass data between Activities or Fragmetns. I don't implement Parcelable. I use Serializable because is more faster to implement.

Example with Serializable:

Model

public class Model implements Serializable {
    private String name;

    public Model(){

    }

    public String getName() {
        return name;
    }

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

    protected Model(Parcel in) {
        name = in.readString();
    }
}

MainActivity

Model currentObject = new Model();
        currentObject.setName("Testing arguments");

        TestFragment fragment = new TestFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("SampleObject");

TestFragment

public class TestFragment extends Fragment {

    Model model;

    public TestFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = this.getArguments();
        if (bundle != null) {
            model = (Model) bundle.getSerializable("SampleObject");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
         View v = inflater.inflate(R.layout.fragment_test, container, false);
         TextView tvArgument = v.findViewById(R.id.tvTestArgument);
         tvArgument.setText(model.getName());
         return v;
    }
}

I hope it helps you.




回答3:


You need to change assignment to class you want to use rather than parent class to receive bundle data,

Edit here:

Change to

ContentMainFragment fragment = new ContentMainFragment(); //Updated

Instead of

 Fragment fragment = new ContentMainFragment(); //Old


来源:https://stackoverflow.com/questions/52172309/parcelable-object-passing-from-android-activity-to-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!