0

I'm wondering if there is a way to restore, after rotation, my list of results without retaining the fragment.

Basically I have a fragment which calls, through a presenter, some api (using RxJava and Retrofit). I added pagination so I can make a call only when I need more data scrolling down.

I'm in the following scenario: - I scroll the list down in order to call the second page from the web - I rotate the screen

In this case what I would need is to show all the items, from page 1 and 2, and then scrolling to the correct position (for this I can use the onSaveInstanceState of the LayoutManager).

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    FwApplication.component(getActivity()).inject(this);

    if (savedInstanceState != null) {
        mRxRunning = savedInstanceState.getBoolean(EXTRA_RX);
        mQuery = savedInstanceState.getString(QUERY_ARG);
        mCurrentPage = savedInstanceState.getInt(CURRENT_PAGE);
    } 
}

@Override
public void onSaveInstanceState(Bundle outState) {
    outState.putString(QUERY_ARG, mQuery);
    outState.putInt(CURRENT_PAGE, mCurrentPage);
    outState.putParcelable(LAYOUT_STATE, mRecyclerView.getLayoutManager().onSaveInstanceState());
    super.onSaveInstanceState(outState);
}

@Override
public void onResume() {
    super.onResume();
    mPresenter.onResume(mRxRunning, mCurrentPage);
}

Is there a way to save the items without calling setRetainInstance? Moreover I would avoid to call the api in order to get all the items back.

The items are POJOs so it won't be a list of simple strings.

user1341300
  • 375
  • 1
  • 5
  • 19
  • Just do the same thing with the items put into the bundle in onSavedInstanceState and retrieve them in onCreate. – aminner May 20 '16 at 13:11
  • The thing is, I'm not using a simple list of strings but a list of objects I created. I edited my question. So the onSaveInstanceState supports only Parcelable objects – user1341300 May 20 '16 at 14:30
  • Actually I'm trying to extend the Parcelable class and see what happens – user1341300 May 20 '16 at 14:32
  • http://stackoverflow.com/questions/3323074/android-difference-between-parcelable-and-serializable As long as your objects in the list are serializable you should be able to add it to the bundle. I run into issues when I forget to extend serializable on the objects but once that's fixed everything is fine. – aminner May 23 '16 at 11:05
  • @user1341300 I have that exact same question. Did you ever find a solution? – bernardo.g Mar 29 '18 at 19:56

1 Answers1

-1

Try to edit the Manifest file. Add this line to block with activity:

android:configChanges="keyboardHidden|orientation|screenSize"> 

It will prevent Activity restart on such events (specially on orientation change)