Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragments are getting overlapped on back button

I have created 3 Fragments namely (FragmentA, FragmentB, FragmentC) and one MainActivity. There is a button in each fragment which replaces itself with next Fragment till FragmentC.

I am replacing FragmentA (with) FragmentB (then with) FragmentC.

Transaction from FragmentA to FragmentB uses below function

    @Override
    public void fragmentreplacewithbackstack(Fragment fragment, String tag) {

        FragmentManager fragmentManager=getSupportFragmentManager();
        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.contner,fragment , tag);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();   
//      fragmentManager.executePendingTransactions();

    }

Transaction from FragmentB to FragmentC uses below function

public void fragmentreplace(Fragment fragment,String tag){
        FragmentManager fragmentManager=getSupportFragmentManager();
        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.contner,fragment , tag);
        fragmentTransaction.commit();   

    }

problem is when i press back button from FragmentC, FragmentC and FragmentA overlap with each other.

As below screen

like image 937
rajahsekar Avatar asked Feb 27 '14 12:02

rajahsekar


People also ask

How to handle back button in Android fragments?

Answer Wiki. To handle Back Button in Android Fragments, you should consider addToBackStack() method as part of your FragmentTransaction.

What is a fragment overlap attack?

A Fragment Overlap Attack, also known as an IP Fragmentation Attack, is an attack that is based on how the Internet Protocol (IP) requires data to be transmitted and processed. These attacks are a form of Denial of Service (DoS) attack where the attacker overloads a network by exploiting datagram fragmentation mechanisms.

How do I use the stacked fragments?

Each fragment includes a button and an input text box. When clicking the button it will either show a hidden or create a new target fragment. When clicking the back menu, the stacked fragments will be popup by order, if the fragment is hidden in the stack ( fragment two ), then the text in the input box will be saved also.

How to use back stack popup to save fragments?

If you add one Fragment into back stack, when you press the android device back menu, you can find the Fragment that is saved in back stack popup. Until all saved Fragments in back stack popup, then the activity will exit. This example contains one activity and three fragments. Each fragment include a button and an input text box.


1 Answers

You need to add Fragment C to backstack as well if you wanna go to Fragment B on back press from here.

So call the below for Fragment C as well.

 fragmentTransaction.addToBackStack(null);

EDIT - Change this current method you are using to go from B to C,

public void fragmentreplace(Fragment fragment,String tag){
    FragmentManager fragmentManager=getSupportFragmentManager();
    FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.contner,fragment , tag);
    fragmentTransaction.addToBackStack(null); //this will add it to back stack
    fragmentTransaction.commit();   
}
like image 114
Atul O Holic Avatar answered Oct 19 '22 23:10

Atul O Holic