Ahmad Alkhatib
Ahmad Alkhatib

Reputation: 1226

Prevent re-added a fragment once it's loaded

So here is my navigationDrawer in image below has menu items ,and every single item add a fragment and load it own data , so how to prevent re-added a fragment once it's loaded, it's re-added the fragment and i don't want that.

enter image description here

here is what i tried :

 @Override
public boolean onNavigationItemSelected(MenuItem menuItem) {

    menuItem.setChecked(true);
    if (menuItem.getItemId() == R.id.nav_home) {

        makeTransaction(new Home(), "home_fragment");
        toolbarTitle.setText("Home");

    } else if (menuItem.getItemId() == R.id.nav_prizes) {

        makeTransaction(new Prizes(), "prizes_fragment");
        toolbarTitle.setText("Prizes");

    } else if (menuItem.getItemId() == R.id.nav_myAccount) {

        mDrawerLayout.closeDrawer(GravityCompat.START);
        startActivity(new Intent(MainActivity.this, MyAccount.class));

    } else if (menuItem.getItemId() == R.id.nav_contact_us) {

        makeTransaction(new ContactUs(), "contactUs_fragment");
        toolbarTitle.setText("Contact Us");

    } else if (menuItem.getItemId() == R.id.nav_about_us) {

        makeTransaction(new AboutUs(), "aboutUs_fragment");
        toolbarTitle.setText("About Us");

    } else if (menuItem.getItemId() == R.id.nav_share) {
        mDrawerLayout.closeDrawer(GravityCompat.START);
    } 
    return false;
}                              

public void makeTransaction(Fragment fragment, String tag) {
    FragmentTransaction transaction = getFragmentManager().beginTransaction();

    Fragment test = getFragmentManager().findFragmentByTag(tag);
    getFragmentManager().executePendingTransactions();

    if (test != null && test.isVisible()) {
        mDrawerLayout.closeDrawer(GravityCompat.START);
    } else if (test != null && test.isAdded()) {
        getFragmentManager().popBackStack(tag,0);

    } else {          
        mDrawerLayout.closeDrawer(GravityCompat.START);
        transaction.setCustomAnimations(R.anim.enter_from_left,
                R.anim.exit_to_right, 0, 0);                      
        transaction.add(R.id.fragment_container, fragment, tag);
        transaction.addToBackStack(tag);          
        transaction.commit();
    }                               
}           

Upvotes: 2

Views: 1365

Answers (2)

Movsar Bekaev
Movsar Bekaev

Reputation: 908

Declare variable for FragmentManager outside of the methods:

FragmentManager fm;

Inside somewhere in onCreate(), you initialize it.

fm = getFragmentManager();

In your makeTransaction() use fm instead of "getFragmentManager()"

 FragmentTransaction transaction = fm.beginTransaction();

and etc..

This make your management easier, and you can find fragments using TAG like that:

 Fragment fragment = ({FRAGMENT_CLASS}) fm.findFragmentByTag({TAG});

    if (fragment == null) {
        //add to transaction
    } else {
        //use existing thorugh fragment
    }

If after that the fragment isn't null it'll have the link to the fragment which is created using that TAG.

if you create following fragment:

  makeTransaction(new ContactUs(), "contactUs_fragment");

You would be able to get reference to it like this:

Fragment fragment = (ContactUs) fm.findFragmentByTag("contactUs_fragment");

and do with it anything you want.

Upvotes: 0

Amarjit
Amarjit

Reputation: 4357

I face similar problem that on clicking same item again and again it reload same fragment every time

For this you can make method

public boolean checkFragment(int position, Fragment mFragment) {
        if ((mFragment instanceof "your_fragment_name"))
            return true;


        return false;
    }

and before committing fragment

you can add this

if (checkFragment(position, mFragment)) return;

As

if (checkFragment(position, mFragment)) return;
        transaction.setCustomAnimations(R.anim.enter_from_left,
                R.anim.exit_to_right, 0, 0);
        transaction.replace(R.id.fragment_container, fragment, tag);
        transaction.addToBackStack(tag);            
        transaction.commit();

EDIT

Please replace your makeTransaction method with below code.

public void makeTransaction(Fragment fragment, String tag) {
    FragmentTransaction transaction = getFragmentManager().beginTransaction();

    Fragment test = getFragmentManager().findFragmentByTag(tag);
    if (test != null && test.isVisible()) {
        mDrawerLayout.closeDrawer(GravityCompat.START);
    } else if (test != null) {
        getFragmentManager().popBackStack(tag,      FragmentManager.POP_BACK_STACK_INCLUSIVE);
        // getSupportFragmentManager().popBackStackImmediate(tag, 0);
    } else {          
        mDrawerLayout.closeDrawer(GravityCompat.START);


if (checkFragment(position, mFragment)) return;
     transaction.setCustomAnimations(R.anim.enter_from_left,
                R.anim.exit_to_right, 0, 0);
        transaction.replace(R.id.fragment_container, fragment, tag);
        transaction.addToBackStack(tag);          
        transaction.commit();
    }             
}       

If source is an object variable, then instanceof is a way of checking to see if it is a Fragment or not.

So we use this property to check that if currently clicked item is instance of its fragment or not.

Upvotes: 1

Related Questions