0

For a week I have been searching and trying but I cannot find a proper solution for the following Android issue. (This is how I see it now and it may be the wrong approach)

We have a MainActivity and 3 Fragments: SingleAnswer, MultipleAnswer and ImageAnswer. The MainActivity retrieves an object with a random amount of questionTypes. The MainActivity uses starts a fragment based on the retrieved object.

How can I iterate through Fragments?

For example: First there is a MultipleAnswer Fragment and a answer is selected by the user. Then the next Fragment has to be loaded...

I don't know if this approach is right or if I have to look for something else than Fragments.

Rich
  • 21
  • 1
  • 5
  • You can `replace` the fragment depending on the object what you received. You can also use `getFragmentById()` or `getFragmentByTag()` to retrieve the fragment if it is already there. – Gaurav Gupta Sep 23 '13 at 14:10

2 Answers2

1

Someone also faced this problem. At the first glance there does not seem to be a getFragments method. However you can use a workaround as specified here:

Is there a way to get references for all currently active fragments in an Activity?

Community
  • 1
  • 1
productioncoder
  • 4,225
  • 2
  • 39
  • 65
0

it is not real to iterate fragments that was not added, only replace will be good choice or
try to use ViewPager for it with adapter example: http://geekyouup.blogspot.com/2011/07/viewpager-example-from-paug.html in the main activity initialize your adapter like this one :

 mContainer = (ViewPager) mainLayout.findViewById(R.id.pager_container);
    myPager = mContainer.getViewPager();
    adapter = new MyPager(myPager,new String[]{"R.layout.first","R.layout.second","...."});

myPager.setAdapter(adapter);
myPager.setCurrentItem(0); 
EugenUngurean
  • 429
  • 3
  • 14