1

I am using navigation drawer in my android app and when i am reselecting fragment it loads twice.
Here is my code

    private void displayView(int position) {
    // update the main content by replacing fragments
    Fragment fragment = null;
    switch (position) {
    case 0:
        // if(fraghome!=0)
        // {
        // fraghome=0;
        fragment = new HomeFragment();
        // }
        break;
    case 1:
        fragment = new BlogsFragment();
        break;
    case 2:
        fragment = new NewsFragment();
        break;
    case 3:
        fragment = new TransferFragment();
        break;
    case 4:
        fragment = new FixturesFragment();
        break;
    // case 5:
    // fragment = new BestXIFragment();

    // break;
    case 5:
        fragment = new FeedFragment();
        break;
    case 6:
        fragment = new TwitterFragment();
        break;
    case 7:
        fragment = new FacebookFragment();
        break;
    case 8:
        fragment = new BookmarkFragment();
        break;
    default:
        break;
    }

    if (fragment != null) {

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.replace(R.id.frame_container, fragment).commit();

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(navMenuTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}

In each of fragment i am using async task and when i am selecting fragment the async task started again and again
please help me

Yogi
  • 63
  • 2
  • 14
  • where do you call `displayView` in your code? – mmlooloo Aug 16 '14 at 08:11
  • * */ private class SlideMenuClickListener implements ListView.OnItemClickListener { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { // display view for selected nav drawer item displayView(position); } } – Yogi Aug 16 '14 at 08:18
  • can you explain `when i am reselecting fragment it loads twice.`? what do you actually do ? can you tell the story? – mmlooloo Aug 16 '14 at 09:14
  • wherever i am selecting fragment async task started loading again – Yogi Aug 16 '14 at 09:20
  • thank you! very good news!!can you post where do you call async task? – mmlooloo Aug 16 '14 at 09:24
  • can you give me your email so i can mail you my fragment – Yogi Aug 16 '14 at 09:38
  • have look at this http://stackoverflow.com/questions/24186781/android-navigation-drawer-fragment-state – Achin Sep 18 '14 at 09:20

1 Answers1

0

Normally in fragment, fragmens are destroyed, if it is not the current top fragment or the fragment just right or left to the current top fragment. So the asynctask is getting called again and again. I assume you are getting some data using the asynctask to show in the fragment. So try to make the data source variable static and check null for the data source variable before executing asynctask. if the data source is not null then show the data. I have added some abstruct code for your understanding. For better understanding you can see this link. Hope it helps. Thanks.....

In each fragment.....

public class TabFragment extends Fragment{
    private static DataSource ds = null;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        /*
        .
        other view initializing work
        .
        */
        if(ds == null){
            //execute asynctask and set data to ds in asynctask
        } else {
            // set data to view.
        }
        return view;
    }
}
Community
  • 1
  • 1
Ashiq
  • 430
  • 1
  • 9
  • 24