0

I think this question was asked many times, but no other question somehow answers my issue. The problem is very simple. I have an activity with fragments

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_tabbed);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
    }
private void setupViewPager(ViewPager viewPager) {
        adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new Fragment1(), "1");
        adapter.addFragment(new Fragment2(), "2");
        adapter.addFragment(new Fragment3(), "3");
        viewPager.setAdapter(adapter);
    }

so far so good, I can navigate to fragment 2 and there I can start a new activity

Intent intent = new Intent(activity, AnotherActivity.class);
startActivity(intent);

Then, when I finish doing stuff in this anotheractivity, can use the back button to return to my activity with fragments. But then, it always starts with fragment1 instead of fragment2, the one I started the anotheractivity. How can I make it return to the original state with fragment2 active?

I tried onSaveStateInstance and PendingActivity, but both in vain.

rst
  • 2,510
  • 4
  • 21
  • 47
  • I never meet this before. Maybe you made something wrong at some place. Maybe you can post more code related to jump between 2 Activity. – Dennis Lu Jan 15 '16 at 10:09

3 Answers3

1

Just simply do the following, on onStop state save the current position and onStart state change ViewPager item position.

  int selectedPosition=0;

    @Override
    public void onStart(){
        super.onStart();
        viewPager.setCurrentItem(selectedPosition);
    }

    @Override
    public void onStop(){
        super.onStop();
        selectedPosition=viewPager.getCurrentItem();
    }
Bharatesh
  • 8,943
  • 3
  • 38
  • 67
  • That doesn't work, onStart nor onStop are never called :( – rst Jan 16 '16 at 09:07
  • how is that possible if you are starting another (B) activity from one (A)activity. then (A) activity must call stop() and when you come back to (A) onStart() will call. – Bharatesh Jan 16 '16 at 10:18
  • I don't know! I just added a breakpoint to the onStop method, but it never reaches that point :( (it does others though) – rst Jan 16 '16 at 10:30
  • So I found the solution, first: onStop is not suitable, since it might happen, that onStop is never executed when returning too fast. onPause is better. Then I change in the manifest `android:launchMode="singleTop"`, maybe that helped also – rst Jan 17 '16 at 05:19
  • A new activity only pauses (not stops) the underlying one when it doesn't totally block its visibility. [See Activity life cycle](http://developer.android.com/training/basics/activity-lifecycle/pausing.html) – NameSpace Jan 17 '16 at 07:47
1

If the new activity is only putting your current one in the Paused or Stopped state then the recreation event begins at the "onResume" or "onStart" methods respectively. In this scenario, everything stays in memory that wasn't explicitly released in "onPause" or "onStopped."

  • onPause - called if second activity only partially covers first.
  • onStop - called if second activity completely covers first.

Thus, when your activity isn't released from memory, the only way its state changes is if one of these methods are changing it -- which they might be doing unintentionally, by overwriting the current state with initialization values.

If the new Activity is actually destroying your activity, then recreation event is the "onCreate" and the activity was not preserved in memory. Instead, as you are aware, the onSaveInstance method was called and the default implementation writes data about the ViewHiearchy.

This ViewHiearchy data includes the ViewPager and info about the fragments. The super method in the onCreate will create new fragment instances and attach them, so you should try that and see if it gives the desired results.

Currently you are overwriting the onCreate's attempt to restore using the onSaveInstance bundle, because you set your default initialization on every call. You should try implementing it as shown in the developer's guide by checking for a non-null bundle and skipping default initialization. (See http://developer.android.com/training/basics/activity-lifecycle/recreating.html):

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); // Always call the superclass first

    // Check whether we're recreating a previously destroyed instance
    if (savedInstanceState != null) {
        // Restore value of members from saved state
        mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
        mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
    } else {
        // Probably initialize members with default values for a new instance
    }
    ...
}

Also checkout how the second answer here restores the state of the ViewPager in the onCreate: ViewPager and fragments — what's the right way to store fragment's state?

Community
  • 1
  • 1
NameSpace
  • 10,009
  • 3
  • 39
  • 40
0

How can I make it return to the original state with fragment2 active

Use intent.putExtra(); to pass the position of the fragment which you want to display, then in the called Activity, use this method setCurrentItem(position); of ViewPager and then the specific Fragment will be shown.

ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108