Not to reload the fragment once loaded-ANDROID
NickName:shivani gupta Ask DateTime:2016-06-20T14:07:50

Not to reload the fragment once loaded-ANDROID

In my Android app I have 6 fragments which are replaced one by one on clicking item in navigation drawer. My problem is that fragment are loaded again and again when clicking on navigation drawer items. I don not want my fragment to reload again and again. Instead I want them to remain in their previous state when switching from fragment to fragment. Addbacktostack() is not working.please tell me some other way to do it:

My code is:

 @Override
public void onDrawerItemSelected(View view, int position) {
    row_view = view;
    displayView(position);

}

private void displayView(int position) {
    Fragment fragment = null;
    String title = getString(R.string.app_name);
    switch (position) {
        case 0:
            fragment = new Fragmenta();
            title = "aaaa";
            toolbar_title.setText(title);
            break;
        case 1:
            fragment = new Fragmentb();
            title = "Introduction";
            toolbar_title.setText(title);
            break;
        case 2:
            fragment = new Glossary();
            title = "Glossary";
            toolbar_title.setText(title);
            break;
        case 3:
            fragment = new SettingsFragment();
            title = "Settings";
            toolbar_title.setText(title);
            break;
        case 4:
            fragment = new FeedbackFragment();
            title = "Feedback";
            toolbar_title.setText(title);
            break;
        case 5:
            fragment = new Fragmentc();
            title = "cc";
            toolbar_title.setText(title);
            break;

        default:
            break;
    }



if (myfragmentstate != null) {             // This is always null
        //Restore the fragment's instance
        fragment = getSupportFragmentManager().getFragment(myfragmentstate, "mContent");
        Log.i("sadfas","fragment"+fragment);

    }
    if (fragment != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager
                .beginTransaction();
        fragmentTransaction.replace(R.id.container_body,fragment);   //always reloads a fragment and i donot want this
        fragmentTransaction.commit();
        toolbar_title.setText(title);

    }
}



@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    Log.i("MicroRAE","instanmce save"+fragment);
    getSupportFragmentManager().putFragment(outState, "mContent", fragment);
}

Copyright Notice:Content Author:「shivani gupta」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/37915563/not-to-reload-the-fragment-once-loaded-android

Answers
shivani gupta 2016-06-22T06:22:09

I have found a very good solution :\n\nString backStateName = fragment.getClass().getName();\n\n FragmentManager fragmentManager = getSupportFragmentManager();\n boolean fragmentPopped = fragmentManager.popBackStackImmediate (backStateName, 0);\n\n if (!fragmentPopped){ //fragment not in back stack, create it.\n FragmentTransaction fragmentTransaction = fragmentManager\n .beginTransaction();\n fragmentTransaction.replace(R.id.container_body,fragment);\n fragmentTransaction.addToBackStack(backStateName);\n fragmentTransaction.commit();\n toolbar_title.setText(title);\n }\n",


Galder 2019-04-22T11:44:44

Very easy: Just declare a global String that saves your last fragment loaded. In the method where you load your fragment, just compare if it´s the same. If not, load the new fragment, if matches, just do nothing.\n\n String lastFragmentLoaded = null; // THIS VARIABLE IS GLOBAL\n\n //YOUR METHOD THAT LOAD FRAGMENTS\n LoadMyFragment() \n {\n String currentFragmentName = yourFragment.getClass().getName();\n\n if(lastFragmentLoaded != currentFragmentName) {\n //LOAD YOUR FRAGMENTS\n\n //FINALLY, SAVE YOUR LAST FRAGMENT LOADED TO COMPARE NEXT TIME\n lastFragmentLoaded = currentFragmentName;\n }\n else \n {\n // LET IT EMPTY IF YOU WANT\n }\n }\n",


More about “Not to reload the fragment once loaded-ANDROID” related questions

Not to reload the fragment once loaded-ANDROID

In my Android app I have 6 fragments which are replaced one by one on clicking item in navigation drawer. My problem is that fragment are loaded again and again when clicking on navigation drawer i...

Show Detail

Reload Fragment

I am just curious, as I have set up Fragment tabs in a FragmentActivity, how to reload a fragment either from the FragmentActivity, or from the Fragment itself, to just purge and reload that tab. I

Show Detail

How to limit tap on BottomNavigatioView item to only once, to not reload fragment?

How can I limit a tap on a BottomNavigatioView item to just once, so that my mapfragment doesn't have to reload itself when someone spamms the item? My current Solution is this one: fragmentManag...

Show Detail

How to store fragments in a stack? So once a fragment X is loaded , moving from another fragment Y to X does not "reload" X

How to store fragments in a stack? So once a fragment X is loaded, moving from another fragment Y to X does not "reload" it but get the fragment from some kind of saved state... Trying to reduce t...

Show Detail

Refresh Fragment at reload

In an android application I'm loading data from a Db into a TableView inside a Fragment. But when I reload the Fragment it displays the previous data. Can I repopulate the Fragment with current data

Show Detail

Reload a Fragment

everyone, my issue is : I'm in ProfilFragment, that was generated with the Menu Drawer it extends Fragment, and I can't change this (it broke some other part). When I click on a button I want to

Show Detail

Reload Fragment after popBackStack

I need reload fragment after this process Go from Fragment A to Fragment B and go from Fragment B to Fragment C, then do the registration process, and after the registration is correct, popBackStack

Show Detail

Fragment crashing on reload

My Problem is, on the second reload my fragment is crashing with a NullPointerException. The exception leads to FragmentTransaction ft = getFragmentManager().beginTransaction(); LogCat: java.lang.

Show Detail

How to reload Fragment?

Here is my situation : I'm retrieving data from a database in a fragment and the result is asynchronous. So I want to reload the view after I get result from database. I've tried many ways with "

Show Detail

Android - Reload Fragment into a ViewPager

I'm building an App which extracts data from a SQLite local database. I have an Activity, in which I use a ViewPager; in this ViewPager, there are 12 swipable Fragments. I'd like to implement a "...

Show Detail