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.
android android-fragments
Warpzit May 29 '12 at 11:46 a.m. 2012-05-29 11:46 a.m.
source share