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

时间:2018-09-04 18:45:22

标签: android android-intent bundle parcelable

我正在尝试将implements Parcelable的对象从活动传递到片段。我知道如何从活动传递到活动。我只想试试这个。但是,当我收到对象时,它总是收到null。我该如何解决这个问题?

currentObject是实现Parcelable的类的对象实例

ContentMainFragment是Fragment类

在活动中

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

在片段中

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

谢谢。

3 个答案:

答案 0 :(得分:1)

尝试一下

首先,改变一些小东西。

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(); 
}

第二,您的方法似乎没有问题,然后再次检查您是否在Activity中解析了有效对象(currentObject)。

答案 1 :(得分:0)

我向您展示一些代码来帮助您:

模型

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;
    }
}

此代码正常工作。

但是通常情况下,我必须在“活动”或“碎片”之间传递数据。我没有实现Parcelable。我使用Serializable是因为它实现起来更快。

具有可序列化的示例:

模型

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;
    }
}

希望对您有帮助。

答案 2 :(得分:0)

您需要将分配更改为要使用的类,而不是父类,以接收bundle数据,

在此处编辑:

更改为

ContentMainFragment fragment = new ContentMainFragment(); //Updated
  

代替

 Fragment fragment = new ContentMainFragment(); //Old