Junaid
Junaid

Reputation: 7860

Fragment refreshing on backpress

I have an activity MainActivity there are three fragments associated with this activity.

Now one of my fragment Timeline has a listview. Which I populate from a Database in the backend. I use an AsyncTask to fetch values from the DB and process them to the List. I trigger this AsyncTask in the onCreate of the Fragment Timeline.

Now from Timeline on click of any list item I navigate to a different Activity called as DetailActivity

The problem is whenever I press back from the DetailActivity the onCreate of my MainActivity is called and my list refreshes again - the whole DB operation is called again and my list does not retain its state.

I am calculating the visible items of my List before I navigate away from the Fragment but I am forced to use static values for these variables so that I retain the position. How to avoid this?

Below are the snippets of my onPause and onResume as laid down in the fragment Timeline

    static  int index;
    static int top;
    @Override
    public void onPause(){

        System.out.println("onPause");
        index = lv.getFirstVisiblePosition();
        View v = lv.getChildAt(0);
        top = (v == null) ? 0 : v.getTop();
        super.onPause();
        uiHelper.onPause();
    }


@Override
    public void onResume(){
        super.onResume(); 
        //dbHelper.open();
        System.out.println("onResumr");
        lv.setSelectionFromTop(index, top);
        ActionBar actionBar = getActivity().getActionBar(); 
        actionBar.setTitle("Timeline"); 
        uiHelper.onResume();
        AppEventsLogger.activateApp(getActivity());
        updateUI();

    }

This also forces my AsyncTask to run again and again, which is an overhead.

Edit:

The root of this problem - After struggling for so many days I borrowed a friends phone to test and all was sorted on this new phone. I found out that I had turned on the Do not keep Activities option in my Developer Settings. The Dumb me!!

Upvotes: 0

Views: 162

Answers (2)

Yash Sampat
Yash Sampat

Reputation: 30611

This is, unfortunately, the default behavior of the Fragment class. A Fragment is destroyed whenever the containing Activity is paused, and recreated whenever the containing Activity is resumed. If you use an Activity instead of a Fragment for the list, you would not experience the same behavior. With an Activity:

  • AsyncTasks and/or web services would not be called again.
  • The list would show the previously scrolled position.

If you want the same behavior with a Fragment, you need to override the onSaveInstanceState() method. And while interesting, it is not a small amount of work.

EDIT:

Make sure the Do not keep Activities option is unselected in your phone's Developer Settings. This, though, does not change the essential behavior of the Fragment class that I have outlined above.

Upvotes: 1

You can call setRetainInstance(true) on your fragment. The lifecycle will be slightly different though.

A nice view of a fragment's lifecycle is available here http://corner.squareup.com/2014/10/advocating-against-android-fragments.html

Upvotes: 1

Related Questions