简体   繁体   English

Android导航,从片段活动开始,然后再次返回

[英]Android navigation, from fragment activity and back again

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 到目前为止一切顺利,我可以导航至片段2,然后可以开始新的活动

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. 但是,它总是以fragment1而不是fragment2(我启动了otheractivity的开始)开始。 How can I make it return to the original state with fragment2 active? 在fragment2处于活动状态时,如何使它返回到原始状态?

I tried onSaveStateInstance and PendingActivity, but both in vain. 我尝试了onSaveStateInstance和PendingActivity,但都徒劳。

Just simply do the following, on onStop state save the current position and onStart state change ViewPager item position. 只需简单地执行以下操作,就可以在onStop状态保存当前位置,并在onStart状态更改ViewPager项目位置。

  int selectedPosition=0;

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

    @Override
    public void onStop(){
        super.onStop();
        selectedPosition=viewPager.getCurrentItem();
    }

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. 如果新活动仅使您当前的活动处于“已暂停”或“已停止”状态,则重新创建事件分别从“ onResume”或“ onStart”方法开始。 In this scenario, everything stays in memory that wasn't explicitly released in "onPause" or "onStopped." 在这种情况下,所有内容都保留在“ onPause”或“ onStopped”中未明确释放的内存中。

  • onPause - called if second activity only partially covers first. onPause-如果第二个活动仅部分覆盖第一个活动则调用。
  • onStop - called if second activity completely covers first. onStop-如果第二个活动完全覆盖了第一个活动则调用。

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. 如果新的活动实际上正在破坏您的活动,则娱乐活动为“ onCreate”,并且该活动未保留在内存中。 Instead, as you are aware, the onSaveInstance method was called and the default implementation writes data about the ViewHiearchy. 相反,您已经知道,调用了onSaveInstance方法,并且默认实现写入有关ViewHiearchy的数据。

This ViewHiearchy data includes the ViewPager and info about the fragments. 该ViewHiearchy数据包括ViewPager和有关片段的信息。 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. onCreate中的super方法将创建新的片段实例并将其附加,因此您应尝试并查看它是否给出了所需的结果。

Currently you are overwriting the onCreate's attempt to restore using the onSaveInstance bundle, because you set your default initialization on every call. 当前,您正在使用onSaveInstance捆绑包覆盖onCreate的还原尝试,因为您在每个调用上都设置了默认初始化。 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 ): (请参阅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? 还要检查这里的第二个答案如何在onCreate中还原ViewPager的状态: ViewPager和片段—存储片段状态的正确方法是什么?

How can I make it return to the original state with fragment2 active 如何使fragment2激活,使其返回到原始状态

Use intent.putExtra(); 使用intent.putExtra(); to pass the position of the fragment which you want to display, then in the called Activity , use this method setCurrentItem(position); 传递要显示的片段位置 ,然后在调用的Activity ,使用此方法setCurrentItem(position); of ViewPager and then the specific Fragment will be shown. ViewPager ,然后将显示特定的Fragment

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM