1

I am using ViewPager and i am creating Fragments dynamically inside getItem method of FragmentStatePagerAdapter as follows and i want to avoid recreation of Fragment inside this method so that the application does not crash.

        @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.

        Fragment fragment = new Fragment();

        switch (position) {
        case 0:                 
            fragment = new CourseOverViewFragment();
            break;
        case 1:
            fragment = new CourseSchedueListFragment();
            break;
        case 2:
            fragment = new CourseSchedueListFragment();
            break;
        case 3: 
            fragment = new CourseNoteListFragment();
            break;
        case 4:             
            fragment = new ProjectListFragment();
            break;
        default:
            Log.i(StudyManagerDataSource.LOG_TAG, "default");
            break;

        }

        return fragment;

    }

How ever i fond this stackoverflow answer usefull. But i am not able to assign tag inside getItem method.

Thanks!

Community
  • 1
  • 1
Shajeel Afzal
  • 5,913
  • 6
  • 43
  • 70
  • 3
    You shouldn't have to, because the `FragmentPagerAdapter` already attempts to find previously added fragments (by tag, for which the adapter uses a private method) for you. Only if none was found, `getItem()` will be called. With that being said, your application crash is probably the result of something else.. – MH. Jun 19 '13 at 03:49
  • Ok i will debug it and will try my best to find the reason :) – Shajeel Afzal Jun 19 '13 at 03:53

0 Answers0