user3714447 user3714447 - 6 months ago 15
Java Question

Sending data to a Fragment instead of an Activity with a Parcelable object

I want to send data to a Fragment with a Parcelable class I've made.

I know this is how you'd go about things when using an Activity (ObjectA being the class that extends Parcelable):

ObjectA obj = new ObjectA();
// Set values etc.
Intent i = new Intent(this, MyActivity.class);
i.putExtra("com.package.ObjectA", obj);
startActivity(i);


But as you can't use intents with Fragments, I've got no idea how to do this.

Can I even use a Parcelable class? If not, what should I use? I it is possible, how?

Edit/update:

I've updated with your recommendations, but still don't get everything.

This is the fragment we use to create a list view:

package com.tsjd.HotMeals;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.example.tabtest.R;

public class RecipeListViewFragment extends Fragment {

ArrayList<String> titles;
ArrayList<String> descriptions;
ArrayList<String> images;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

RecipeListViewParcer object = getArguments().getParcelable("extraObject");
titles = object.getTitles();
descriptions = object.getDescriptions();
images = object.getImages();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_recipelistview, container,
false);

final ListView listview = (ListView) v.findViewById(R.id.listview);


RecipeListViewAdapter adapter = new RecipeListViewAdapter(
getActivity(), titles);
listview.setAdapter(adapter);

return v;
}

public void onListItemClick(ListView l, View v, int position, long id) {
// do something with the data
}
}


I want to use this ListView for different things, so I want to be able to send the three ArrayLists to it. For this I've made a class that implements Parcelable, this is it:

package com.tsjd.HotMeals;

import java.util.ArrayList;

import android.os.Parcel;
import android.os.Parcelable;

public class RecipeListViewParcer implements Parcelable{

private ArrayList<String> titles;
private ArrayList<String> descriptions;
private ArrayList<String> images;

@SuppressWarnings("unchecked")
private RecipeListViewParcer(Parcel in) {
titles = (ArrayList<String>) in.readSerializable();
descriptions = (ArrayList<String>) in.readSerializable();
images = (ArrayList<String>) in.readSerializable();
}

public int describeContents() {
return 0;
}

public ArrayList<String> getTitles()
{
return titles;
}

public void setTitles(ArrayList<String> titles){
this.titles = titles;
}

public ArrayList<String> getDescriptions()
{
return descriptions;
}

public void setDescriptions(ArrayList<String> descriptions){
this.descriptions = descriptions;
}

public ArrayList<String> getImages()
{
return images;
}

public void setImages(ArrayList<String> images){
this.images = images;
}

public void writeToParcel(Parcel out, int flags) {
out.writeSerializable(titles);
out.writeSerializable(descriptions);
out.writeSerializable(images);
}

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

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



}


Finally, this is a Fragment which uses the listview to show favourite recipes, here I want to send three ArrayLists, this is the only code that currently has errors. I don't know how to declare the Parcel I need for the constructor of my Parcelable class, how should I go about this? (I haven't put anything in my ArrayLists yet, but I hope the idea is clear).

package com.tsjd.HotMeals;

import java.util.ArrayList;

import android.os.Bundle;
import android.os.Parcel;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

public class MyRecipesFragment extends Fragment {


ArrayList<String> titles = new ArrayList<String>();
ArrayList<String> descriptions = new ArrayList<String>();
ArrayList<String> images = new ArrayList<String>();



Parcel in = new Parcel();

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment fragment = new RecipeListViewFragment();

RecipeListViewParcer obj = new RecipeListViewParcer(in);

obj.setDescriptions(descriptions);
obj.setTitles(titles);
obj.setImages(images);

Bundle bundle = new Bundle();
bundle.putParcelable("extraObject", obj);
fragment.setArguments(bundle);

fragmentTransaction.commit();
}

}

Answer

You have to use the FragmentManager:

Send the 3 ArrayLists in Activity:

       FragmentManager fragmentManager = getFragmentManager();
       FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
       Fragment fragment = new ViewControllerFragment();

       ObjectA obj = new ObjectA();
       Bundle bundle = new Bundle();
       bundle.putStringArrayList("Titles", obj.getTitles());
       bundle.putStringArrayList("Descriptions", obj.getDescriptions());
       bundle.putStringArrayList("Images", obj.getImages());
       fragment.setArguments(bundle);

       fragmentTransaction.commit();

Get Object in Fragment:

Bundle bundle = getArguments();                    
 titles = bundle.getStringArrayList("Titles");            
descriptions= bundle.getStringArrayList("Descriptions");            
images= bundle.getStringArrayList("Images");

Or just try other Posts: Send data from activity to fragment in android

Comments