Replaced Fragments are still active

I am writing an Android App using the support version of Fragments (android.support.v4.app.Fragment).

I have a strange bug in my code and I dont know how to fix it. When I replace a Fragment with an other one, the replaced one is still active and receiving touch events. A tap on the location of a Button from the replaced Fragment will still fire an OnClick Event.

I really dont know how to fix this. Can anybody help me?

Java Code

Fragment newFragment = new LoginActivityRegisterFragment();

...

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.replace(R.id.login_fragment, newFragment);
ft.commit();

...

XML Layout

...

<fragment
    class="de.myapp.fragments.LoginActivityMainFragment"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/login_fragment" />

...

Answers


You can replace fragments in a container view only. This means you need a container (not a fragment itself).

<FrameLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/container" />

Then you can initialize it by adding a fragment to it.

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.add(R.id.container, new LoginActivityMainFragment());
ft.commit();

And then you can replace it.

Fragment newFragment = new LoginActivityRegisterFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.replace(R.id.container, newFragment);
ft.commit();

Need Your Help

WP7 image binding

image binding windows-phone-7

I have a Windows phone 7 application which contains a listbox. I want, for each item in the listbox to have a particular image.

Get content of certain xml node based on its attribute in PHP

php html xml

I have an XML sheet I want to extract content from.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.