android – Bring to front existing fragment-ThrowExceptions

Exception or error:

I have an application with n buttons. Each button create a new fragment and attach it to a view. So if i touch button A, B, C in that order the fragments FA, FB and FC are created and stacked:


Now if i press A again a new fragment is created and stacked, so the stack become:


Is there a way to retreive an existing fragment from the stack (if exist) and bring it to front? So if i have the stack


and press the “A” button again, the FA fragment is removed from backstack and put to front:


and if I press “C” the stack become


Thanks in advance.

How to solve:

To achieve this you need to obtain parent View where you placed your Fragment, or the root of your Fragment. Once you have it, just call the bringToFront() method that every View has, and you’ll move that View to the front.

You could get your View or the container View of your View with the findViewById() method of your context.

Leave a Reply

Your email address will not be published. Required fields are marked *