Asaf
Asaf

Reputation: 2035

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?

Upvotes: 0

Views: 2524

Answers (1)

user
user

Reputation: 87064

That's a bad way of accessing the ViewPager from MainActivity(I hope MainActivity.instance isn't a static field holding a reference to a MainActivity instance). You should let the MainActivity completely handle the ViewPager's position:

In the onPause() of the MainActivity you would keep the current code. In the onResume() method of the MainActivity you would have:

@Override
public void onResume() {
    super.onResume();
    vpPref = getPreferences(MODE_PRIVATE);
    final int value = vpPref.getInt("viewPagerPage");
    final boolean shouldRestore = vp.getboolean("restoreStatusFlag");
    // we are again in the MainActivity so also make 
    // restoreStatusFlag  as false in the preferences
    if (shouldRestore) {
        mPager.setCurrentItem(value);  
    }
}

The shouldRestore preference would be set from the launched activity in the onCreate. This way if MainActivity is started you'll keep the default page(shouldRestore will be false as the launched activity didn't run yet). If after MainActivity you go to the launched activity shouldRestore will become true(you need to set it in the onCreate) and as you come back the ViewPager will be set to the right page in onResume().

This leaves one scenario where the user could go to the launched activity(the shouldRestore will be true) but never comes back to MainActivity with BACK. For this you could test the category from the Intent that started MainActivity(which I'm assuming is the one mentioned in the manifest as the launcher activity):

Set<String> catg = getIntent().getCategories();
for (String cat : catg) {
     if (cat.equals(Intent.CATEGORY_LAUNCHER)) {
        // reset the shouldRestore flag to false so we end up with the default page 
        break;
     }
}

Upvotes: 1

Related Questions