savedInstanceState总是空的,但的onSaveInstanceState()始终调用savedInstanceState、onSaveInstanceState

由网友(げ邂逅ぜ)分享简介:首先我是新来的Andr​​oid - 这个问题可能看起来愚蠢的:)的我已经创建了一个包含ViewPager的主要活动。该ViewPager链接到我的动作条的标签。目前,我的主要活动包括这样三个标签。I've created an main Activity that contains a ViewPager....

首先我是新来的Andr​​oid - 这个问题可能看起来愚蠢的:)的

我已经创建了一个包含ViewPager的主要活动。该ViewPager链接到我的动作条的标签。目前,我的主要活动包括这样三个标签。

I've created an main Activity that contains a ViewPager. The ViewPager is linked to my ActionBar tabs. Currently my main Activity contains 3 tabs in this way.

我的问题是这样的:当我第三个标签上,并创建一个新的活动,后来回到我的主要活动,我总是回到我的第一个标签。

My issue is the following: when I'm on the 3rd tab and create a new Activity and afterwards return to my main Activity I always return to my 1st tab.

我已经试过以下方法在我的主要活动:

I've tried the following approach in my main Activity:

// debugger breakpoints tell me this method is always called when other activity is shown ...
@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    int pos = getActionBar().getSelectedNavigationIndex();
    outState.putInt("tab", pos);
}

// however, in onCreate() my savedInstanceState is always 'null' ...
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.invalidateOptionsMenu();

    setContentView(R.layout.activity_main);

    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    mSectionsPagerAdapter = new SectionsPagerAdapter(
            getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    mViewPager
            .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    actionBar.setSelectedNavigationItem(position);
                }
            });

    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        actionBar.addTab(actionBar.newTab()
                .setText(mSectionsPagerAdapter.getPageTitle(i))
                .setTabListener(this));
    }

    // trying to restore state here, yet savedInstanceState is _always_ null ...
    if (savedInstanceState != null) {
        actionBar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
    }
}

我读过的savedInstanceState将始终为空时,有没有明确的标识。我的XML布局有虽然定义一个ID,所以这不应该是一个问题。我也看到了'的onSaveInstanceState()`方法应该总是调用第一个超类中的方法,所以我相应的执行方法。

I've read that the savedInstanceState will always be null when there's no id defined. My XML layout has an id defined though, so this should not be an issue. I've also read that the 'onSaveInstanceState()` method should always call the method on the super class first, so I've implemented the method accordingly.

任何人有任何想法如何解决我的问题?如何让ViewPager,以显示正确的选项卡/片段时,主要活动是重现?

Anyone have any idea how to fix my issue? How to get the ViewPager to show the right tab / fragment when the main Activity is recreated?

推荐答案

感谢的她微笑的,您的建议,帮我解决我的问题,因为它帮我找到我的问题的根源。

Thanks She Smile, your suggestions helped me solve my issue, because it helped me find the source of my problem.

显然的onCreate()只能有一个 savedInstanceState 参数,当活动被彻底摧毁和重建。在我的情况下活动依然存在,虽然,所以我不会有一个 savedInstanceState 参数。我的修复是创建一个静态int包含在所选的选项卡索引。我知道这可能不是最好的解决办法,因为这个变量将在整个活动的所有实例共享,而是因为在我的情况就只有甚至我的活动的一个实​​例应该没事。

Apparently onCreate() could only have a savedInstanceState parameter when the Activity was completely destroyed and recreated. In my case the Activity still existed though, therefore I will not have a savedInstanceState parameter. My "fix" was to create a static int that contains the selected tab index. I realise this might not be the best solution, since this variable will be shared across all instances of the Activity, but since in my situation there will only even be one instance of my activity it should be fine.

public class MainActivity extends Activity implements ActionBar.TabListener {
    private static int tabIdx;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (savedInstanceState != null) {
            tabIdx = savedInstanceState.getInt("tab", 0);
        }
        getActionBar().setSelectedNavigationItem(tabIdx);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        tabIdx = getActionBar().getSelectedNavigationIndex();
        outState.putInt("tab", tabIdx);
    }
}
阅读全文

相关推荐

最新文章