ViewPager and fragments — what’s the right way to store fragment’s state?

When the FragmentPagerAdapter adds a fragment to the FragmentManager, it uses a special tag based on the particular position that the fragment will be placed. FragmentPagerAdapter.getItem(int position) is only called when a fragment for that position does not exist. After rotating, Android will notice that it already created/saved a fragment for this particular position and … Read more

Communicating between a fragment and an activity – best practices

The easiest way to communicate between your activity and fragments is using interfaces. The idea is basically to define an interface inside a given fragment A and let the activity implement that interface. Once it has implemented that interface, you could do anything you want in the method it overrides. The other important part of … Read more

How to handle button clicks using the XML onClick within Fragments

I prefer using the following solution for handling onClick events. This works for Activity and Fragments as well. public class StartFragment extends Fragment implements OnClickListener{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_start, container, false); Button b = (Button) v.findViewById(R.id.StartButton); b.setOnClickListener(this); return v; } @Override public void onClick(View v) … Read more

Update ViewPager dynamically?

When using FragmentPagerAdapter or FragmentStatePagerAdapter, it is best to deal solely with getItem() and not touch instantiateItem() at all. The instantiateItem()–destroyItem()–isViewFromObject() interface on PagerAdapter is a lower-level interface that FragmentPagerAdapter uses to implement the much simpler getItem() interface. Before getting into this, I should clarify that if you want to switch out the actual fragments … Read more

Duplicate ID, tag null, or parent id with another fragment for com.google.android.gms.maps.MapFragment

The answer Matt suggests works, but it cause the map to be recreated and redrawn, which isn’t always desirable. After lots of trial and error, I found a solution that works for me: private static View view; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (view != null) { ViewGroup parent = … Read more

How to implement a ViewPager with different Fragments / Layouts

As this is a very frequently asked question, I wanted to take the time and effort to explain the ViewPager with multiple Fragments and Layouts in detail. Here you go. ViewPager with multiple Fragments and Layout files – How To The following is a complete example of how to implement a ViewPager with different fragment … Read more

Passing data between a fragment and its container activity

Try using interfaces. Any fragment that should pass data back to its containing activity should declare an interface to handle and pass the data. Then make sure your containing activity implements those interfaces. For example: JAVA In your fragment, declare the interface… public interface OnDataPass { public void onDataPass(String data); } Then, connect the containing … Read more