Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to give initial data to fragments?

So I've learned that I need an empty constructor in order for my fragments not to crash on reinitialization. My problem is that I use lists of data for my fragments when they are initialized (at least some of them). So what would be a good way to start new fragments with a list of data. Should I in the OnCreate() make a getData method which gets the data from some other source or what would be a proper approach?

Feeding the bundle with data really wouldn't be a very good approach as I have a lot of data.

So let's take a case (I understand it tons better that way).

When a user clicks on a button the fragment is started. What I used to do was creating a new fragment this way:

    FragmentManager fragmentManager = getSupportFragmentManager();     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();          fragmentTransaction.replace(R.id.center_container, new DetailFragment(item));     fragmentTransaction.addToBackStack(DETAIL_TAG);          fragmentTransaction.commit(); 

Then in my fragment:

public DetailFragment(EventItem item) {     mItem = item;     mPlaces = Database.getContainerPlaces(getActivity()).getValidItems(); } 

I can't give all the data to a bundle, so that wouldn't work. So what should I do?

A: Should I initialize the fragment with the empty constructor and then from my activity use setters to set the data directly in the fragment? However, won't I be missing data if the user presses home, Android closes the fragment and the user later returns?

B: Should I initialize the fragment with factory pattern and call setRetainInstance(true), give the fragment a key for identifying the data, and then letting the fragment fetch the data needed in onCreateView from some third source?

C: Should I just make an empty constructor and then in onCreate() fetch the data needed for the fragment?

It should be noted that the app is locked in portrait so the issue is primarily with maintaining the objects when Android closes and the user restarts.

like image 266
Warpzit Avatar asked May 29 '12 11:05

Warpzit


People also ask

How do you pass information between fragments?

When working with child fragments, your parent fragment and its child fragments might need to share data with each other. To share data between these fragments, use the parent fragment as the ViewModel scope.

How do you initiate a fragment?

Add a fragment to an activity You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.

Which method of a fragment should be called to send the data?

Sending data from a Fragment to its host Activity Call the interface callback method to pass data as a parameter.

Which method is used to load fragments?

Such as listed below: onCreate - This method is called to initialize or add the fragment to the host activity. The layout is also inflated in this stage. All elements initialized in this method are usually preserved whenever the fragment is paused.


1 Answers

So what would be a good way to start new fragments with a list of data.

Use the factory pattern and the "arguments" Bundle, such as:

package com.commonsware.empublite;  import android.os.Bundle;  public class SimpleContentFragment extends AbstractContentFragment {   private static final String KEY_FILE="file";    protected static SimpleContentFragment newInstance(String file) {     SimpleContentFragment f=new SimpleContentFragment();      Bundle args=new Bundle();      args.putString(KEY_FILE, file);     f.setArguments(args);      return(f);   }    @Override   String getPage() {     return(getArguments().getString(KEY_FILE));   } } 

If you are retaining your fragment instance, you should be able to get away with just using ordinary setters to put stuff in data members. The "arguments" Bundle is retained as part of configuration changes, so for non-retained instances, this is the way to ensure your setup data is retained if the user rotates the screen, etc.

like image 92
CommonsWare Avatar answered Sep 18 '22 21:09

CommonsWare