Bidhan
Bidhan

Reputation: 10687

How to stop the currently selected Fragment from reloading in a Navigation Drawer

I have a Navigation drawer with 4 items each of which load 4 different fragments when clicked. All fragments contain a listview that displays data fetched from the web using an AsyncTask. My problem is this: Let's say I click on the second item and that fragment loads just fine. But when I click the same item again, the fragment also loads again. Is there a way I can stop the currently selected fragment from loading again? Maybe there's some way I can disable the currently selected item from being clicked. I'm sure there must be a better way. Thanks.

Upvotes: 2

Views: 1133

Answers (1)

reVerse
reVerse

Reputation: 35264

You're probably adding your Fragment to a FrameLayout or any other kind of container. Before starting the transaction and replacing the Fragment in the container you can check if the Fragment you're trying to add is present already via the following code:

private void openFragment(Fragment newFragment){
    Fragment containerFragment = getFragmentManager().findFragmentById(R.id.container);
    if (containerFragment.getClass().getName().equalsIgnoreCase(newFragment.getClass().getName()))
       return;
    else
       // Start transaction and replace fragment
}

Upvotes: 5

Related Questions