android如何存储状态的页导航 view 屏幕旋转

标签: Java Android
发布时间: 2017/3/19 17:28:22
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我想要添加到屏幕旋转支持 activity 拥有一部传呼机 view 。 想要的什么是当用户更改屏幕方向,传呼机中的选项卡 view 那是开放应在打开 activity 重新启动和每个 time 第一个选项卡打开.请帮我。 新方向。 但现在在此先感谢。

package com.example.ali.namallibrary;
import android.app.Fragment;
import android.content.Context;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.io.Serializable;

public class aboutLibrary extends AppCompatActivity {

CustomAdapter customAdapterpter = null;
TabLayout tabLayout;
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_about_library);
    customAdapterpter  = new CustomAdapter(getSupportFragmentManager(),getApplicationContext());

    viewPager = (ViewPager) findViewById(R.id.viewPager);
    viewPager.setAdapter(customAdapterpter);

    tabLayout = (TabLayout) findViewById(R.id.tabBar);
    tabLayout.setupWithViewPager(viewPager);
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener()   {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }
    });
} // end of oncreate

private class CustomAdapter extends FragmentPagerAdapter {
    private String[] fragmentNames = {"About","Collection","Timing","Contact"};
    public CustomAdapter(FragmentManager supportFragmentManager, Context applicationContext) {
        super(supportFragmentManager);
    }
    @Override
    public android.support.v4.app.Fragment getItem(int position) {
        switch (position)
        {
            case 0 :
                return new aboutLibraryFrag();
            case 1 :
                return new libraryCollectionFrag();
            case 2 :
                return new libraryTimingFrag();
            case 3 :
                return new contactUsFrag();
            default:
                return null;
        }
    }
    @Override
    public int getCount() {
        return fragmentNames.length;
    }
    @Override
    public CharSequence getPageTitle(int position) {
        return fragmentNames[position];
    }
}

解决方法 1:

每当你旋转屏幕, activity 是完全被毁,重新创建。要处理这种情况,还有生命周期方法称为 onSaveInstanceState 。在此方法中,您可以保存 Bundle 对象。A Bundle 是键/值对,你定义你想周围旋转后的一群。在这 Bundle ,你可以存储当前的状况你 view 传呼机。我没有工作了太多的 ViewPagers,但getCurrentItem可能是你在寻找什么。你需要让你 ViewPager 成员变量和常数的关键。然后我猜它会看起来像︰

public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the user's current game state
    savedInstanceState.putInt(CURRENT_POSITION_KEY, mViewPager.getCurrentItem());

    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

onCreate方法接受 Bundle savedInstanceState -如果 activity 正在重新启动从旋转,此参数将拥有一切你保存在 onSaveInstanceState 。因此你可以这样做的 onCreate :

// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
 int currentPos = savedInstanceState.getInt(CURRENT_POSITION_KEY);
 mViewPager.setCurrentItem(currentPos)
}

有关加载和保存之间的旋转状态的详细信息可以在这里找到︰ https://developer.android.com/guide/components/activities/activity-lifecycle.html#saras

官方微信
官方QQ群
31647020