Alisha Sharma
Alisha Sharma

Reputation: 49

Navigating to previous fragment without reloading it

I am creating an app in which I'm using fragments. I want to go back to the previous fragment from another fragment without reloading the previous fragment.

Code for going to the next fragment:

Fragment newFragment;
android.support.v4.app.FragmentManager manager = ((AppCompatActivity) context).getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
newFragment = new Inner_details();
transaction.replace(R.id.content_data, newFragment);

transaction.addToBackStack("fragment6");

transaction.commit();

Code for going to the previous fragment

FragmentManager fm = getActivity().getSupportFragmentManager();

fm.popBackStack ("fragment6", FragmentManager.POP_BACK_STACK_INCLUSIVE);

Upvotes: 3

Views: 7818

Answers (4)

Bhunnu Baba
Bhunnu Baba

Reputation: 1802

Use Add() instead of replace() for opening previous fragment without reloading it.

 fragmentManager.beginTransaction().add(R.id.contentPanel, fm)
                    .addToBackStack(null).commit();

and also do some changes in method of handling back press :

1) On fragment ->

view.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
                //((MainActivity) getActivity()).finish();
                if(getFragmentManager().getBackStackEntryCount() > 0) {
                    getFragmentManager().popBackStack();
                }else {
                    ((MainActivity) getActivity()).finish();
                }
                return true;
            }
            return false;
        }
    });

2) On Activity ->

@Override
public void onBackPressed()
{
    if(getFragmentManager().getBackStackEntryCount() > 0)
        getFragmentManager().popBackStack();
    else
        super.onBackPressed();
}

Upvotes: 0

MohammedAli
MohammedAli

Reputation: 2519

try this

the 1st one is store all fragement

transaction.addToBackStack(null);

and the second one remove all fragments into the stack

    getActivity().getSupportFragmentManager().popBackStack();

Upvotes: 2

morteza moradi
morteza moradi

Reputation: 305

You Should Use right from addToBackStack method.

Managing Fragment Backstack

A record of all Fragment transactions is kept for each Activity by the FragmentManager. When used properly, this allows the user to hit the device’s back button to remove previously added Fragments (not unlike how the back button removes an Activity). Simply call addToBackstack on each FragmentTransaction that should be recorded:

// Create the transaction
FragmentTransaction fts = getSupportFragmentManager().beginTransaction();
// Replace the content of the container
fts.replace(R.id.flContainer, new FirstFragment()); 
// Append this transaction to the backstack
fts.addToBackStack("optional tag");
// Commit the changes
fts.commit();

Programmatically, you can also pop from the back stack at any time through the manager:

FragmentManager fragmentManager = getSupportFragmentManager();
if (fragmentManager.getBackStackEntryCount() > 0) {
    fragmentManager.popBackStack();
}

With this approach, we can easily keep the history of which fragments have appeared dynamically on screen and allow the user to easily navigate to previous fragments.

Example:

This is a Senario : when we are in login fragment and user click on backbutton home fragment should be load:

when you click on button replace a fragment login inside a Framelyout :

fragmentManager.beginTransaction().replace(R.id.your_placeholder, new Fragment_Login()).addToBackStack("login").commit();

and when you click on back button in application first Check what is last fragment if last fragment ( fragment that is in last backStack ) is login replace fragment home on Framelayout :

public void onBackPressed() {

        int index = fragmentManager.getBackStackEntryCount() - 1;
        String tag = fragmentManager.getBackStackEntryAt(index).getName();
//        Fragment fragment = fragmentManager.findFragmentByTag(tag);

        switch (tag) {
            case "login":
                fragmentManager.beginTransaction().replace(R.id.your_placeholder, new HomePage_Fragment()).addToBackStack("home").commit();
                break;  

}

for more info and example Look This Sites :

https://www.survivingwithandroid.com/2013/04/android-fragment-transaction.html

Upvotes: 0

Meikiem
Meikiem

Reputation: 1936

you are replacing your fragments so the previous fragment will no longer be available with its content, you might try to load your fragments by "add" instead of "replace":

 transaction.add(R.id.content_data, newFragment);

Upvotes: 1

Related Questions