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.