RTF
RTF

Reputation: 6504

Controlling android fragment creation order

So, I've got 3 fragments in a FragmentActivity. I have an xml layout file with some containers etc. so I setContentView(R.layout.my_activity) and then programmatically add the 3 fragments. 2 of these fragments are used with a FragmentPagerAdapter, so that I can swipe between them.

Now, I need the 2 swipe-able fragments to be created before the other fragment (e.g. the onCreateView methods of fragments 1 and 2 should be called before the onCreateView method of fragment 3). However, fragment 3 is always being created first. The code:

public class MyActivity extends FragmentActivity {

private ViewPager viewPager;
private MyPagerAdapter pagerAdapter;
private Fragment1 fragment1;
private Fragment2 fragment2;
private Fragment3 fragment3;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_activity);
    initViewPager();
    initFragment3();
}

private void initViewPager() {
    List<Fragment> pagerFragments = new LinkedList<Fragment>();
    fragment1 = (Fragment1) Fragment.instantiate(this, Fragment1.class.getName());
    fragment2 = (Fragment2) Fragment.instantiate(this, Fragment2.class.getName());
    pagerFragments.add(fragment1);
    pagerFragments.add(fragment2);
    pagerAdapter = new MyPagerAdapter(getSupportFragmentManager(), pagerFragments);
    viewPager = (ViewPager) findViewById(R.id.fragment_pager);
    viewPager.setAdapter(pagerAdapter);
}

private void initStatsFragment() {
    fragment3 = new Fragment3();
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.add(R.id.fragment3_container, fragment3);
    transaction.commit();
}
    ...

I've also tried the above code with this replacement for the initViewPager method:

...
...
private void initViewPager() {
    List<Fragment> pagerFragments = new LinkedList<Fragment>();
    fragment1 = new Fragment1();
    fragment2 = new Fragment2();
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.add(R.id.swipe_fragment_container, fragment1);
    transaction.add(R.id.swipe_fragment_container, fragment2);
    transaction.commit();
    pagerFragments.add(fragment1);
    pagerFragments.add(fragment2);
    pagerAdapter = new MyPagerAdapter(getSupportFragmentManager(), pagerFragments);
    viewPager = (ViewPager) findViewById(R.id.fragment_pager);
    viewPager.setAdapter(pagerAdapter);
}
    ...
    ...

but this produces an error and crashes on launch, similar to the following:

01-30 15:05:06.250: E/AndroidRuntime(2772): java.lang.IllegalStateException: Can't change container ID of fragment Fragment1{416461c8 #1 id=0x7f0b0003 android:switcher:2131427332:0}: was 2131427331 now 2131427332

Can anyone tell me how I might accomplish what it is that I'm trying to do, or suggest something that I could try? I should also mention that re-factoring such that fragment dependency actions are carried out from their constructors is not an option.

Upvotes: 2

Views: 1083

Answers (1)

RTF
RTF

Reputation: 6504

As indicated in the comments below my question, I decided that re-factoring my code to remove dependencies between fragments was the best way to go. I couldn't/shouldn't rely on what's going on under the hood (i.e. Android fragment instantiation/creation order) for my code to run as I expect it too.

Upvotes: 2

Related Questions