Use snippets for each view in the pager.
write the code below in the onCreate() method of FragmentActivity .
List<Fragment> fragments = new Vector<Fragment>(); //for each fragment you want to add to the pager Bundle page = new Bundle(); page.putString("url", url); fragments.add(Fragment.instantiate(this,MyFragment.class.getName(),page)); //after adding all the fragments write the below lines this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(), fragments); mPager.setAdapter(this.mPagerAdapter);
Sample fragment definition:
public class MyFragment extends Fragment { public static MyFragment newInstance(String imageUrl) { final MyFragment mf = new MyFragment (); final Bundle args = new Bundle(); args.putString("somedata", "somedata"); mf.setArguments(args); return mf; } public MyFragment() {} @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String data = getArguments().getString("somedata"); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
I follow this method whenever I need to use ViewPager . Hope this helps. I could not understand why your instance creation method was called twice from the information you provided.
SKK
source share