Starting a new activity from ViewPager and going back to the same page
In my app there is a viewpager with 3 fragments in it.
In one of the fragments I'm starting another activity.
I want the user to go back to the same viewpager page he were in before on
back/up.
I implemented it successfully using the activity that hosts the
viewpager's onPause and onResume methods. It worked but the problem was
that onResume was being fired after onCreate, which resulted in the app
starting at the same page (instead of a "default", different page I set in
the onCreate method).
I then tried to place the onResume code in the launching activity's
onOptionsItemSelected method, but that didn't work at all.
Launched activity:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
vpPref = getPreferences(MODE_PRIVATE);
int value = vpPref.getInt("viewPagerPage", -1);
if (value != -1) {
MainActivity.instance.mPager.setCurrentItem(value);
vpPrefEditor = vpPref.edit();
vpPrefEditor.remove("viewPagerPage");
vpPrefEditor.commit();
}
return true;
}
return super.onOptionsItemSelected(item);
}
Main Activity: (hosts the viewpager)
public void onPause() {
super.onPause();
vpPref = getPreferences(MODE_PRIVATE);
vpPrefEditor = vpPref.edit();
vpPrefEditor.putInt("viewPagerPage", mPager.getCurrentItem());
vpPrefEditor.commit();
}
The problem is in the first code. I don't know whether this is a placement
problem or getting the wrong instance of MainActivity that is wrong...
What could be the cause to this behavior?
No comments:
Post a Comment