user1730789
user1730789

Reputation: 5177

Add fragments into a Fragment Activity

I have a FragmentActivity that contains a FrameLayout. I use the following code to add Fragments to the Fragment Activity.

FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.fragment_content, fragment, fargmentTag);
        ft.addToBackStack(null);
        ft.commit();

Now R.id.fragment_content is FrameLayout. This adds a Fragment onto the view. But the underlying view still remains visible. Meaning the one lying view is seen through the new fragment ? What am i doing wrong.

Kind Regards

Upvotes: 1

Views: 2724

Answers (4)

tejinder
tejinder

Reputation: 61

Using ft.replace will replace any previously added fragment with this fragment but if the activity has some view added to its layout rather than a fragment ,then the fragment added through add or replace will show its content over it rather than replacing it as it is not a fragment. either add another layout within your main layout and assign it some id and then add the fragment to that container. Hope it helps.

Upvotes: 0

ernestkamara
ernestkamara

Reputation: 73

ft.replace() should work fine, otherwise you have to update your question for a better understanding.

Upvotes: 1

vinoth
vinoth

Reputation: 485

for the next fragment use ft.replace(); so the previous one will be repalced.

Upvotes: 1

laalto
laalto

Reputation: 152917

Either ft.remove() the old fragment, or use ft.replace() instead of ft.add().

Upvotes: 1

Related Questions