Osborne Cox
Osborne Cox

Reputation: 466

Nav drawer fragment being re-created on selection, resulting in repeated image downloads

My app currently has a Navigation Drawer with a few fragments. The default fragment which loads initially is a profile page which downloads an image (Bitmap) from a server.

When I navigate to another fragment and come back to the Profile fragment, the fragment is re-created and the image has to re-download from the server.

Is there any way to save the state of the Profile Fragment so that I don't have to keep sending a request to the server to re-download the image. Currently the UI feels very clunky due to this repeated download request that needs to be made.

Here is an extract of the code for the Activity which starts the relevant fragment.

public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    Fragment fragment = null;


    switch(position+1) {
        case 1:
            Bundle LoginDetails = getIntent().getExtras();
            String [] login_info = LoginDetails.getStringArray("login_details");
            String username = login_info[0];
            String password = login_info[1];

            fragment = new Profile().newInstance(position + 1, username, password);

            break;

        case 2:
            fragment = new Search().newInstance(position+1);
            break;

        case 3:
            fragment = new NewsFeed().newInstance(position+1);
            break;


        default:
            Log.w(this.getClass().getSimpleName(), "Reached default on Navigation Drawer select item");
            break;
    }

    //fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();

    if (fragment != null) {
        FragmentTransaction fragmentManager = getFragmentManager().beginTransaction();
        fragmentManager.replace(R.id.container, fragment);
        fragmentManager.addToBackStack(null);
        fragmentManager.commit();
    }

    else {
        Log.e("Activity", "Error Creating fragment");

    }
}

My Profile.java file (Fragment) subsequently uses an AsyncTask call in onCreateView to download the Profile Image. Should I be making this call somewhere else or is there some other way to maintain the state of the Fragment's layout?

Upvotes: 0

Views: 103

Answers (1)

romtsn
romtsn

Reputation: 11992

The best soultion is to cache your image locally, you can do it manually, or by using some beautiful libraries such as Picasso or Ion, then when your fragment will be recreated you will get that image from cache.

But if you don't want to use it, there is another way to store your image: you can move your AsyncTask that loads an image to Activity that uses NavigationDrawer, then execute your AsyncTask, for example, in onCreate, store the image in your activity, and when you will replace current fragment to ProfileFragment pass the image as a parameter to newInstance method. Then the image will be stored for your NavigationDrawerActivity lifecycle.

Upvotes: 2

Related Questions