1

I've created my layout with viewPager and TabLayout:

public class HomeFragment extends Fragment
{


public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 3 ;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View inflatedView = inflater.inflate(R.layout.fragment_home, container,false);


    viewPager = (ViewPager) inflatedView.findViewById(R.id.viewpager);
    int [] drawables = {R.drawable.home,R.drawable.street,R.drawable.map};

    Bundle bundle = new Bundle();

    final SearchFragment searchFragment = new SearchFragment();
    final CardFragment cardFragment = new CardFragment();
    final MapFragment mapFragment = new MapFragment();

    viewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager())
    {
        public Fragment getItem(int position)
        {
            if(position == 0)
             return searchFragment;
            else if(position ==1)
             return cardFragment;
            else
             return mapFragment;
        }

        @Override
        public int getCount()
        {
            return int_items;
        }
    });

    tabLayout = (TabLayout) inflatedView.findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);

    for (int i = 0; i < tabLayout.getTabCount(); i++)
    {
        tabLayout.getTabAt(i).setIcon(drawables[i]);
    }

    return inflatedView;

}





    @Override
    public void onAttach(Context context) {

        super.onAttach(context);

    }

    @Override
    public void onDetach() {
        super.onDetach();

    }
}

now I need to do this code in Asynctask (I post only doInBackground() method):

List <ParseObject> result;
ParseQuery<ParseObject> query;

 protected Void doInBackground(final Void... params)
 {
   try
   {
      query = ParseQuery.getQuery("Trains");
      result = query.find();

   }
   catch (com.parse.ParseException e)
   {
    e.printStackTrace();
   }
}

so now I want to pass List <ParseObject> result; at SearchFragment, CardFragment and MapFragment.

It's possible to use a Bundle? Or have I to use other method?

ste9206
  • 1,822
  • 4
  • 31
  • 47

2 Answers2

8

As most of the developers are confused with performance parameters of Serialization and Parcelable, Im putting the explanation below

Now comes how to implement Parceleable interface

Create object class which you want to pass implement Parcelable interface

public class ContactPojo implements Parcelable{
       private String name;
       private String job_title;
       public void setName(String name) {
        this.name = name;
       }

       public void setJob_title(String job_title) {
        this.job_title = job_title;
       }
    public String getName() {
        return name;
    }

    public String getJob_title() {
        return job_title;
    }
    private ContactPojo(Parcel parcel){
        name=parcel.readString();
        job_title=parcel.readString();
    }
    @Override
    public int describeContents() {
        return 0;
    }
    @Override
    public void writeToParcel(Parcel parcel, int flags) {
        parcel.writeString(name);
        parcel.writeString(job_title);
    }
public static final Parcelable.Creator<ContactPojo> CREATOR = new
            Parcelable.Creator<ContactPojo>() {
                public ContactPojo createFromParcel(Parcel in) {
                    return new ContactPojo(in);
                }

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

Now populate the pojo class by the doing the following

ContactPojo contactPojo= new ContactPojo();
contactPojo.setName("name");
contactPojo.setJob_title("name");

send it to next intent by this

Intent intent=new Intent(this, DetailView.class);
intent.putExtra("Data", contactPojo);

Retrieval of data in next intent by next steps

ContactPojo contactPojo=new ContactPojo();
contactPojo=getIntent().getParcelableExtra("Data");
Log.i(AppConstants.APPUILOG, "Name: " + contactPojo.getName() );
Community
  • 1
  • 1
Sreehari
  • 5,621
  • 2
  • 25
  • 59
  • if I use intent means to pass data from activities sure? and if I want to pass data only to fragments? Because I need the same List< > in these three fragments – ste9206 May 13 '16 at 11:06
  • 2
    @Stallion Out of curiosity.. Why not `Serialization` Ain't we use `getArguments()` Instead of getIntent() in Fragments? – Rethinavel May 13 '16 at 11:06
  • 1
    @ste9206 ` roomObject = (ResultPojo) getArguments().getSerializable( "result"); ` you can get the array list like this in fragment. – Rethinavel May 13 '16 at 11:10
  • @RethinavelPillai so I have to use a bundle? – ste9206 May 13 '16 at 11:11
  • 1
    @ste9206 Yes you should.. It is recommended. And i personally feel that `Parcelable` is `Expensive` rather than `serialization`. – Rethinavel May 13 '16 at 11:13
  • 2
    Android standard is Parcelable than Serialization. Serialization is Java standard. http://www.developerphil.com/parcelable-vs-serializable/ – Sreehari May 13 '16 at 11:13
  • @RethinavelPillai and to set Argument? I have to call Bundle bundle = new Bundle( ) and then ? – ste9206 May 13 '16 at 11:17
  • 1
    @ste9206 check my edit specifying performance and implementation simplification with plugins – Sreehari May 13 '16 at 11:22
  • @ste9206 `DocumentFragment docsFragment = new DocumentFragment(); Bundle docsBundle = new Bundle(); docsBundle.putSerializable("roomsAl", roomsAl); docsBundle.putSerializable("documentsAl", documentsAl); docsFragment.setArguments(docsBundle);` – Rethinavel May 13 '16 at 11:23
  • @Stallion Thanks.. I went through the link. It was quite useful. Thanks. My bad i didn't know that.. – Rethinavel May 13 '16 at 11:25
  • @Stallion but I have to use also a bundle? – ste9206 May 13 '16 at 11:52
  • @ste9206 Not mandatory. You can go through the explanation which I have given. Its self explanatory ! – Sreehari May 13 '16 at 12:09
-2

Just do like this i also used it. Create one class of object which list you want to pass

public class PostDetails implements Serializable {
private String postUrl;
private String postType;
public String getPostUrl() {
return postUrl;
}

/**
* 
* @param postUrl
* The post_url
*/
public void setPostUrl(String postUrl) {
this.postUrl = postUrl;
}

/**
* 
* @return
* The postType
*/
public String getPostType() {
return postType;
}

/**
* 
* @param postType
* The post_type
*/
public void setPostType(String postType) {
this.postType = postType;
}

}

In intent just put

List<PostDetails> postDetailses;
intent.putExtra("vdata", (Serializable) postDetailses);

For Receiving at activity side

postDetailsList= (List<PostDetails>) getIntent().getSerializableExtra("vdata");
Mahesh Giri
  • 1,810
  • 19
  • 27