Android - Viewpager and fragments, methods do not work

I have a ViewPager with two Fragments that I create in the onCreate my FragmentActivity .

 private List<Fragment> fragments = new Vector<Fragment>(); fragments.add(Fragment.instantiate(this,Frag_1.class.getName())); fragments.add(Fragment.instantiate(this,Frag_2.class.getName())); this.vPagerAdapter = new Adapt(super.getSupportFragmentManager(),fragments); vPager = (ViewPager) super.findViewById(R.id.pager); vPager.setAdapter(vPagerAdapter); 

My second Fragment has a method inside that I call to update my ListView - refreshList() :

 public class Frag_2 extends Fragment { private ListView list; private ArrayList<data> data; private boolean firstCreation=true; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setRetainInstance(false); } public void onAttach(Activity activity) { // TODO Auto-generated method stub super.onAttach(activity); } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.layout, container, false); list = (ListView) view.findViewById(R.id.lst); //this.setRetainInstance(true); return view; } public void refreshList(ArrayList <data> data){ if(data!=null){ ArrayAdapter<data> adapter = new Item_data_adapter(getActivity(),data); list.setAdapter(adapter);} } } 

Called from my FragmentActivity

 //Something Frag_2 fr = (Frag_2) vPagerAdapter.getItem(1); if (fr.getView() != null) { fr.refreshList(data); } 

It works fine until I change the screen orientation. Correct me if I am wrong, but I was looking for a clock, and I did not find a solution or a good explanation, FragmentActivity is created only once, and Fragments bound to it, but Fragments recreate configuration changes. Now that the orientation is changing, I am not getting View from onCreate , so when I try to get View from Fragment , it returns null, and the refreshList() method is not called. How can i fix this?

+6
source share
3 answers

I fixed the problem as follows: In onCreate of FragmentActivity

 if(savedInstanceState!=null){ frag1 = (frag_1) getSupportFragmentManager().getFragment(savedInstanceState, frag_1.class.getName()); frag2 = (frag_2) getSupportFragmentManager().getFragment(savedInstanceState, frag_2.class.getName()); } else{ frag1 = (frag_1) Fragment.instantiate(this,frag_1.class.getName()); frag2 = (frag_2) Fragment.instantiate(this,frag_2.class.getName()); } fragments.add(frag1); fragments.add(frag2); protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); getSupportFragmentManager().putFragment(outState, frag_1.class.getName(), frag1); getSupportFragmentManager().putFragment(outState, frag_2.class.getName(), frag2); } 

This may not be the best solution in the universe, but it seems like it works ...

+1
source

When you want to update the List, do the following:

 public void setView() { Frag_2 fr = (Frag_2) vPagerAdapter.getItem(1); fragmentManager.beginTransaction().detach(fr).commit(); fragmentManager.beginTransaction().attach(fr).commit(); } 
0
source

If you are using a dynamic fragment, you first need to check to prevent the creation of a second instance of the fragment.

To check if the system is recreating activity, check to see if the Bundle argument has been passed to your onCreate() null actions.

If it is not null , the system recreates the activity. In this case, the action automatically re-creates the fragments.

If it is null , you can safely instantiate a dynamic fragment. For instance:

 public void onCreate(Bundle savedInstanceState) { // ... if (savedInstanceState != null) { FragmentManager fragmentManager = getFragmentManager() // Or: FragmentManager fragmentManager = getSupportFragmentManager() FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); ExampleFragment fragment = new ExampleFragment(); fragmentTransaction.add(R.id.fragment_container, fragment); fragmentTransaction.commit(); } } 

The Fragment class supports the onSaveInstanceState(Bundle) method (but not the onRestoreInstanceState() method) in much the same way as the Activity class.

  • The default implementation saves the state of all fragment views with identifiers.
  • You can override this method to store fragment status information.
  • If the system re-creates the fragment from the previous saved state, it provides a link to the Bundle containing this state to the onCreate() , onCreateView() and onActivityCreated() ; otherwise, the argument is null .

If you want detailed information, here is a good talk by Ken Jones from Maracan

-1
source

Source: https://habr.com/ru/post/924895/


All Articles