1

I have an activity with a relative layout; And have two different fragments. I show one fragment and switch to the next fragment on a button click vice verse.

// When button menu is clicked

OnClickListener btnClick = new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        if(currentType==0){
            InitThisFragment(1);
        }else{
            InitThisFragment(0);
        }
    }
};

public void InitThisFragment(int type){
        Fragment newFragment;
        if(type==0){
            newFragment=new MainFragment();
        }else{
            newFragment=new JourneyFragment();
        }
        currentType=type;
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.abs_fragment_container, newFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }

R.id.abs_fragment_container is a Relative Layout

BOTH THE FRAGMENT CONTAIN GOOGLE MAPS INSIDE THEM;

But it is not working as expected.

  • By default i add the first fragment [which works fine].
  • Then after an onClick event the 2nd fragment comes[which also works fine]
  • But on my next click the app crashes;

Please help I am new to Google Maps and Fragments;

user1991
  • 179
  • 1
  • 2
  • 11

1 Answers1

0

yeah of course your app crashes because the fragment with the map already exists look at this thread it helped me a lot: Duplicate ID, tag null, or parent id with another fragment for com.google.android.gms.maps.MapFragment

Especially the second answer with 41 ups is easy to understand

And yes I would have posted this one in a comment if I would have enough reputation for it!

Community
  • 1
  • 1
Jack2711
  • 84
  • 1
  • 10