0

So I've used the same method in Fragment to other Fragments but only the first Fragment which I got it working saved its state and items inside the Adapter.

Here's the code in onViewCreated checking whether the Adapter is null and if not, use the same old adapter :

        if(getArguments() != null){
            Parcelable savedRecyclerLayoutState = getArguments().getParcelable(utilities.BUNDLE_RECYCLER_LAYOUT);
            if (getArguments().getString("finishPost") != null && getArguments().getString("finishPost").equals("true")){
                refreshView();
                lobiProgressBar.setVisibility(View.VISIBLE);
            } else {
                if(savedRecyclerLayoutState == null){
                    lobiAdapter = new LobiAdapter(this.getActivity());
                    lobiAdapter.setDisplay(width, height);
                    recyclerView.setAdapter(lobiAdapter);
                    refreshView();
                } else {
                    if (lobiAdapter != null && lobiAdapter.getItemCount() > 0) {
                        manager.onRestoreInstanceState(savedRecyclerLayoutState);
                        if(lobiAdapter == null){
                            lobiAdapter = new LobiAdapter(this.getActivity());
                            lobiAdapter.setDisplay(width, height);
                        }
                        recyclerView.setAdapter(lobiAdapter);
                        lobiProgressBar.setVisibility(View.GONE);
                    } else /*if (lobiAdapter != null && lobiAdapter.getItemCount() == 0)*/{
                        refreshView();
                        lobiProgressBar.setVisibility(View.VISIBLE);
                    }
                }
            }
        } else {
            refreshView();
            lobiProgressBar.setVisibility(View.VISIBLE);
        }

And here's my onPause() method in the same Fragment which saves RecyclerView's state :

@Override
public void onPause() {
    super.onPause();
    getArguments().putParcelable(utilities.BUNDLE_RECYCLER_LAYOUT, recyclerView.getLayoutManager().onSaveInstanceState());
    if(getActivity().getSupportFragmentManager().getBackStackEntryCount() > 1){
        ((NewsfeedActivity)getActivity()).hideToolbarBottom();
    }
}

Weird thing is, it saves the adapter's values, but I don't know when or how. When I debug it, lobiAdapter shows that it has items in it, whereas, other Fragments which uses same method of saving state has no item.

Am I actually doing it wrong or I am unaware of something which I've done to save the state and items?

Kevin Murvie
  • 2,592
  • 1
  • 25
  • 43
  • You should use `onSaveInstanceState(Bundle)` for saving the state. The way you're using `getArguments().putParcelable()`is not how it is intended to be used. – Sufian Jun 21 '16 at 05:44
  • `other Fragments which uses same method of saving state has no item.` what do you mean? Are you trying to save instance of one fragment and accessing inside the other? – Sufian Jun 21 '16 at 05:45
  • @Sufian I'll try using that `onSaveInstanceState`.. I meant that Fragment A can have its contents saved, whereas Fragment B which uses the same method of saving state doesn't have its contents saved – Kevin Murvie Jun 21 '16 at 05:47
  • Check [this question](http://stackoverflow.com/questions/6787071/android-fragment-how-to-save-states-of-views-in-a-fragment-when-another-fragmen). It shows how you can save your fragment's state. Also, your `FragmentA` might not be saving its state at all and you're confusing it because you don't understand the spaghetti-like `Fragment`'s lifecycle, which is understandable. ;) – Sufian Jun 21 '16 at 05:53
  • People said that `onSaveInstance` only called when another activity is started.. I'm now confused lol – Kevin Murvie Jun 21 '16 at 06:06
  • Well, `onSaveInstanceState(Bundle)` is called when the system thinks it is important to save the state, such as when opening new `Fragment` (without closing the current one) or when `Activity`'s `onSaveInstanceState(Bundle)` is called. – Sufian Jun 21 '16 at 06:53

0 Answers0