To be honest, I'm not sure if this should work.  Defining fragments in XML
is mostly intended for things that are going to stay around.  If you are
going to add and remove, you should probably consistently do it dynamically.

I can look into this for a future release, but please note that whatever I
do you can easily get yourself into bad situations using this -- for example
if you don't have a unique containing view group for the fragment, what
happens as you remove/add fragments at that point in the hierarchy is going
to behave inconsistently.

On Wed, Mar 9, 2011 at 2:04 AM, C. Plantier <clemplant...@gmail.com> wrote:

> Hi,
>
> I can't remove a Fragment that was added in an XML layout file, using
> a FragmentTransaction. I don't understand why, I probably
> misunderstood something and I would appreciate some explanations.
>
> ----
> Here's a simple code to demonstrate my problem :
>
>    ** Main layout, main.xml:
>    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/
> android"
>        android:orientation="vertical"
> android:layout_width="fill_parent" android:layout_height="fill_parent"
>        android:id="@+id/root"
>        >
>        <TextView android:id="@+id/hello_view"
>            android:layout_width="fill_parent"
>            android:layout_height="wrap_content"
>            android:text="@string/hello"
>        />
>
>        <fragment android:name="test.fragments.FragmentA"
>            android:id="@+id/fragment_a"
>            android:layout_width="fill_parent"
>            android:layout_height="wrap_content"
>        />
>    </LinearLayout>
>
>    ** Main activity:
>    public class TestFragmentsActivity extends Activity {
>
>        @Override
>        public void onCreate(Bundle savedInstanceState) {
>            super.onCreate(savedInstanceState);
>            setContentView(R.layout.main);
>
>            TextView helloView = (TextView)
> findViewById(R.id.hello_view);
>            helloView.setOnClickListener(new OnClickListener() {
>
>                @Override
>                public void onClick(View v) {
>                    FragmentManager fragmentManager =
> getFragmentManager();
>                    FragmentTransaction transaction =
> fragmentManager.beginTransaction();
>
> transaction.remove(fragmentManager.findFragmentById(R.id.fragment_a));
>                    transaction.commit();
>                }
>            });
>        }
>    }
>
>    ** My fragment class:
>    public class FragmentA extends Fragment {
>
>        @Override
>        public View onCreateView(LayoutInflater inflater, ViewGroup
> container, Bundle savedInstanceState) {
>            return inflater.inflate(R.layout.layout_a, container,
> false);
>        }
>    }
>
>    ** Fragment layout, layout_a.xml:
>    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/
> android" android:layout_width="match_parent"
> android:layout_height="match_parent">
>        <TextView android:text="TextView A" android:id="@+id/
> textView1_a" android:layout_width="wrap_content"
> android:layout_height="wrap_content"></TextView>
>    </LinearLayout>
> ----
>
> When I click on the text view, the fragment should be removed, but
> nothing seems to happen. If I click again, the application crashes
> with the following error messages:
>
>    03-09 09:48:09.248: ERROR/AndroidRuntime(1222):
> java.lang.NullPointerException
>    03-09 09:48:09.248: ERROR/AndroidRuntime(1222):     at
> android.app.BackStackRecord.remove(BackStackRecord.java:365)
>    03-09 09:48:09.248: ERROR/AndroidRuntime(1222):     at
> test.fragments.TestFragmentsActivity
> $1.onClick(TestFragmentsActivity.java:28)
>
>
> Maybe it's not possible to remove fragments that were added using XML
> layouts?
>
> Thanks in advance.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>



-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to