Gvg
Gvg

Reputation: 318

Android replace fragment doesn't hide underlying fragment

I have the following problem:

My app consists of several fragments that are dynammicly added. There is one fragment with a push button and an textedit (called 'fragA').

If I click the push button I want to show an different fragment with some text (called 'fragB'). I do this with the following code (in fragA class):

btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
            Fragment howFragment = new HowFragment();

            FragmentTransaction transaction = null;
            transaction = getFragmentManager().beginTransaction();
            transaction.replace(R.id.flQuestion, howFragment);
            transaction.addToBackStack(null);
            transaction.commit();

            }
        });

Now the problem is this:

When i push the button on fragA the fragment (fragB) is created and show on the screen but when I push on the location where the pushbutton was on fragA it makes an new fragment (fragB). Also if i push where the textedit on fragA was located it opens an keyboard on fragB..

It looks like FragB is just overlaying fragA without replacing it.

I also want to achieve that when i swype to the next fragment that fragB is removed and fragA is just showed normally (state when not pressed button)

Update #

When trying to add and remove this is the following logcat output:

FATAL EXCEPTION: main
java.lang.IllegalArgumentException: No view found for id 0x7f090015      (com.example.eindwerkappv1:id/flQuestion) for fragment HowFragment{419c93c8 #3   id=0x7f090015}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)  
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at   android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429) 
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4441)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
at dalvik.system.NativeStart.main(Native Method)

Upvotes: 2

Views: 7410

Answers (3)

Gvg
Gvg

Reputation: 318

I found the solution now.

I still use the replace method but I had to catch the ontouch event in the new fragment:

Fragment over another fragment issue

See previous link for the answer

Upvotes: 0

nicopico
nicopico

Reputation: 3636

I would guess your fragA fragment is defined in your XML layout?

According to the documentation, this is what happens when you use the <fragment> tag:

the system inserts the View returned by the fragment directly in place of the element.

This is why you cannot remove the previous fragment, as it does not exist. If you want to changes fragment from code, you have to add the first fragment from code too.

You need to use a container like a FrameLayout, and add the first fragment to this container in the onCreate() of your activity, using FragmentTransaction.add().

Then FragmentTransaction.replace() method should work.

Upvotes: 2

Emil Adz
Emil Adz

Reputation: 41099

So why don't you just call the .remove() on the EditText Fragment and .add() the new TextView Framgent instead on osing the .replace()

do some thing like this:

fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(addCommentFragment)
.add(R.id.containerForFragments, commentFragment, "comment"+ 
String.valueOf(numOfComments)).commit();

this worked for me.

Upvotes: 0

Related Questions