Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

issue with .remove (fragments android)

  • 12-03-2013 02:08PM
    #1
    Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,119 Mod ✭✭✭✭


    irrelevant


Comments

  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,119 Mod ✭✭✭✭Tar.Aldarion


    irrelevant


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,119 Mod ✭✭✭✭Tar.Aldarion


    irrelevant


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,119 Mod ✭✭✭✭Tar.Aldarion


    Ok ignore everything else, here is the current problem:


    I want to press a button and it adds/removes a fragment. The first press adds a fragment correctly. However if I try remove the fragment every fragment apart from the one I want removed is removed, why?
    Button2 fragment:
    
     Button button = (Button) view.findViewById(R.id.button2);
            button.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
    
                  ButtonFragment fragment = new ButtonFragment();
                  if (fragment != null && fragment.isVisible()) {
    
    
    
                      FragmentManager fragmentManager = getFragmentManager();
    
                      FragmentTransaction transaction =  fragmentManager.beginTransaction(); 
                      transaction.remove(fragment).commit();
    
                  }
                  else if(!fragment.isVisible())
                  {
                      FragmentManager fragmentManager = getFragmentManager();
    
                      FragmentTransaction transaction =  fragmentManager.beginTransaction(); 
                      transaction.add(R.id.fragment_container, fragment ).commit();
      }       
    
              }
            });
            return view;
          }
    }
    


    I have two fragments like this in xml: When I click the button I want the fragment not defined in xml to be added, and it is. However the next time i press the button, which should remove that fragment. Everything is removed apart from that fragment.
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:background="#123456"
        android:id="@+id/fragment_container"  >
    
    
        <fragment
            android:id="@+id/TimeFragment"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="match_parent"
            class="com.example.myfragment.TimeFragment" >
            <!-- Preview: layout=@layout/details -->
        </fragment>
    
    
    
        <fragment
            android:id="@+id/Button2Fragment"
            android:layout_width="0dp"
            android:layout_weight="3"
            android:layout_height="match_parent"
            class="com.example.myfragment.Button2Fragment" >
            <!-- Preview: layout=@layout/details -->
        </fragment>
    
    </LinearLayout>
    


    Should the code for add/removing fragments be in the fragment doing it like I have now, or the main activity?


  • Closed Accounts Posts: 816 ✭✭✭Opinicus


    Anytime I create/remove a fragment I do it in the Activity/FragmentActivity not the fragment itself.


  • Registered Users, Registered Users 2 Posts: 18,272 ✭✭✭✭Atomic Pineapple


    Opinicus wrote: »
    Anytime I create/remove a fragment I do it in the Activity/FragmentActivity not the fragment itself.

    As above, I haven't got to use Fragments as must as I would like but the general rule seems to be use the FragmentActivity to communicate changes.


  • Advertisement
Advertisement