The correct way to provide source data to fragments?

So, I found out that I need an empty constructor so that my fragments do not crash during reinitialization. My problem is that I use data lists for my fragments when they are initialized (at least some of them). So, what would be a good way to launch new snippets with a list of data. Should I create a getData method in OnCreate () that gets data from some other source or what will be right?

Serving the data packet really would not be a very good approach, since I have a lot of data.

So, let's take a case (as I understand it, this is better).

When the user clicks on the button, the fragment starts. What I did was create a new fragment as follows:

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 snippet:

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

I cannot pass all the data to a packet, so this will not work. So what should I do?

A: Should I initialize the fragment using an empty constructor, and then from my setting to use activity to set the data directly in the fragment? However, will I not be missing data if the user clicks on the house, the android closes the fragment and the user returns later?

B: Should I initialize the fragment using the factory template and call setRetainInstance (true), provide the fragment with a key to identify the data, and then allow the fragment to extract the data needed in onCreateView from some third source?

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

It should be noted that the application is locked in the portrait, so the problem is primarily associated with saving objects when Android closes and the user restarts.

+55
android android-fragments
May 29 '12 at 11:46
source share
1 answer

So, what would be a good way to launch new snippets with a list of data.

Use the factory pattern and Bundle "arguments", for example:

 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 save an instance of a fragment, you should get away from it just by using regular setters to put stuff into data members. The β€œarguments” of the Bundle are saved as part of the configuration changes, so for non-saved instances this is a way to ensure that your setup data is saved if the user rotates the screen, etc.

+68
May 29 '12 at 11:52
source share



All Articles