简体   繁体   English

可包裹对象从android活动传递到片段

[英]Parcelable object passing from android activity to fragment

I'm trying to pass an object that implements Parcelable from an activity to a fragment. 我正在尝试将implements Parcelable的对象从活动传递到片段。 I know how to pass from activity to activity. 我知道如何从活动传递到活动。 I just want to try this. 我只想试试这个。 But when I received the object it always received null . 但是当我收到对象时,它总是收到null How can I resolve this problem? 我该如何解决这个问题?

currentObject is the object instance of the class which implements Parcelable currentObject是实现Parcelable的类的对象实例

ContentMainFragment is the Fragment class ContentMainFragment是Fragment类

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. 谢谢。

Try this 尝试这个

First, change some small things. 首先,改变一些小东西。

MainActivity.java 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 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. 其次,看来您的方法似乎没有问题,然后再次检查您是否在Activity中解析了有效对象(currentObject)。

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 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. 我没有实现Parcelable。 I use Serializable because is more faster to implement. 我使用Serializable是因为它实现起来更快。

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 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. 希望对您有帮助。

You need to change assignment to class you want to use rather than parent class to receive bundle data, 您需要将分配更改为要使用的类,而不是父类,以接收bundle数据,

Edit here: 在这里编辑:

Change to 改成

ContentMainFragment fragment = new ContentMainFragment(); //Updated

Instead of 代替

  Fragment fragment = new ContentMainFragment(); //Old 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM