3

I working on a app whitch has 1 activity with 2 buttons (Map and Listview). By clicking on the buttons the fragment into the FrameLayout needs to change from a listview to a mapview (or from map to listview).

I got this working and the fragments are show the data exacly as i want BUT...

I have a few problems by switching between the fragments. I struggled with the code and the best i got working is this:

FragmentManager fragmentManager;
Fragment listOverview;
AlertOverviewMapsFragment mapsOverview;

@Bind(R.id.fr_overviewContainer) FrameLayout fr_overviewContainer;

@OnClick(R.id.btn_list_overview) void openListOverview()
{
    // Add the fragment to the 'fragment_container' FrameLayout
    if (listOverview == null) { listOverview = new AlertOverviewListFragment();}
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fr_overviewContainer.removeAllViews();
    fragmentTransaction.add(R.id.fr_overviewContainer, listOverview).commit();
}

@OnClick(R.id.btn_maps_overview) void openMapsOverview()
{
    // Add the fragment to the 'fragment_container' FrameLayout
    if (mapsOverview == null){        mapsOverview = new AlertOverviewMapsFragment();}
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fr_overviewContainer.removeAllViews();
    fragmentTransaction.add(R.id.fr_overviewContainer, mapsOverview).commit();
}

The problem is when i click a second time on a button the app crashes and i get a java.lang.IllegalStateException: Fragment already added error. I understand that it means that i added the fragment to the manager (or transaction, dunno). I can create or find some workarounds for this but since this is nothing new i'am looking for the usual way to switch (replace) between the 2 fragments.

The fragments contains data which are loaded from the internet, it will be nice when the data is saved and loaded again by reloading the fragment. (so the user sees the old data when it is updated again in the background)

A CLEAR code example would be nice.....


i read something about the fragment manager and transactions and so as i understand the manager makes it posible to add, remove, delete......... fragments from a transaction? so i need 1 transaction per activity? or is this twisted? so the manager contains the fragments? this part is not clear and realy complicated for me. I'am happy when someone can help me with a solution for my problem and it would be nice when someone can explain how the transactions and the managers work in a realy SIMPLE way becouse i'am just started with programming Android apps.

Thanks for reading my problem and lot more thanks for the people who takes the time and patience for writing an answer!

CodeNinja
  • 836
  • 1
  • 15
  • 38
  • 1
    instead of remove/add use replace – F43nd1r Feb 26 '16 at 14:56
  • I tryed this and it works, thanks! (that i didn't find this by my self....) when someone could answer my second part of the question (under the line) would be great! – CodeNinja Feb 29 '16 at 08:03

0 Answers0