Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

issue with .remove (fragments android)

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


    irrelevant


Comments

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


    irrelevant


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


    irrelevant


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,110 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