0

I have a couple of fragments and a view pager. The user can of course go back and forth in between the pages in the view pager. My problem is that when the user changes the orientation, the current page loses all it's data! . BUT when the user pages back and forth, the data is retrieved again and the page is filled with correct data.

I have tested suggestions in this and this post. None worked.

Here my code:

public class PropertyDetailActivity extends AppCompatActivity {
    Toolbar toolbar;
    long[] mPropertiesIDs;
    private ViewPager mPager;
    private PagerAdapter mPagerAdapter;

    @Override
    public void onSaveInstanceState(final Bundle outState) {
        super.onSaveInstanceState(outState);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        long ID = getIntent().getExtras().getLong("ID");
        mPropertiesIDs = getIntent().getExtras().getLongArray("propertiesIDs");
        setContentView(R.layout.activity_property_detail);
        setupToolbar();
        mPager = (ViewPager) findViewById(R.id.pager);
        mPagerAdapter = new PropertyDetailPagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(mPagerAdapter);
        int index = PropertyArrayList.getIndexOf(mPropertiesIDs, ID);
        mPager.setCurrentItem(index);
    }

    private void setupToolbar() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        final ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);// will make the icon clickable and add the < at the left of the icon.
//        actionBar.setHomeButtonEnabled(true); will just make the icon clickable, with the color at the background of the icon as a feedback of the click.
//        actionBar.setHomeAsUpIndicator(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_property_detail, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


//**********************************************************************************************************************

    private class PropertyDetailPagerAdapter extends FragmentStatePagerAdapter {
        public PropertyDetailPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            mPropertiesIDs = getIntent().getExtras().getLongArray("propertiesIDs");
            PropertyDetailFragment fragment = new PropertyDetailFragment();
            fragment.setPropertyID(mPropertiesIDs[position]);
            return fragment;
        }

        @Override
        public int getCount() {
            return mPropertiesIDs.length;
        }

(I'm not sure if I could express myself good enough since I don't speak English that good. So feel free to edit or leave a comment :))

Thanks for your help in advance.

Community
  • 1
  • 1
Fatima
  • 869
  • 10
  • 35

2 Answers2

0

I have this in one of my applications (Examples of an online course). I can not explain it, but I hope this can help you!

Comments in the code related to the topic

private int mPosition = ListView.INVALID_POSITION;
private static final String SELECTED_KEY = "selected_position";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    [...]

    mPosition = position;

    [...]

    // If there's instance state, mine it for useful information.
    // The end-goal here is that the user never knows that turning their device sideways
    // does crazy lifecycle related things.  It should feel like some stuff stretched out,
    // or magically appeared to take advantage of room, but data or place in the app was never
    // actually *lost*.
    if (savedInstanceState != null && savedInstanceState.containsKey(SELECTED_KEY)) {
        // The listview probably hasn't even been populated yet.  Actually perform the
        // swapout in onLoadFinished.
        mPosition = savedInstanceState.getInt(SELECTED_KEY);
    }

[...]

}

@Override
public void onSaveInstanceState(Bundle outState) {
    // When tablets rotate, the currently selected list item needs to be saved.
    // When no item is selected, mPosition will be set to Listview.INVALID_POSITION,
    // so check for that before storing.
    if (mPosition != ListView.INVALID_POSITION) {
        outState.putInt(SELECTED_KEY, mPosition);
    }
    super.onSaveInstanceState(outState);
}


@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    mForecastAdapter.swapCursor(data);
    if (mPosition != ListView.INVALID_POSITION) {
        // If we don't need to restart the loader, and there's a desired position to restore
        // to, do so now.
        mListView.smoothScrollToPosition(mPosition);
    }
}
compte14031879
  • 1,531
  • 14
  • 27
0

Use this property in your activity tag of your manifest and it will not lose data.

        android:configChanges="orientation|screenSize"
Arslan Ashraf
  • 867
  • 7
  • 12