ViewPager选定的选项卡状态未恢复

时间:2018-09-16 13:00:48

标签: java android android-fragments android-viewpager

我有一个片段,其中包含一个带有5个标签的ViewPager。每当我启动多任务窗格并返回到我的应用程序时,ViewPager都会重置回第一个选项卡。有人知道如何防止这种情况发生吗?我使用的代码由于某些原因无法正常工作。

活动

public class MyActivity extends AppCompatActivity {
    private boolean activityRecreated;

    private static final String TAG = MyActivity.class.getSimpleName();

    // declaring initial value for applying appropriate Theme
    private Boolean mCurrentValue;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        activityRecreated = savedInstanceState != null;

        if (savedInstanceState != null) {
            tabIndex = savedInstanceState.getInt("tabIndex", 0);
        }

        // Checking which Theme should be used. IMPORTANT: applying Themes MUST called BEFORE super.onCreate() and setContentView!!!
        Log.d(TAG, "onCreate:::: retrieving preferences");
        SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        mCurrentValue = mSharedPreferences.getBoolean("my_preference", false);
        Log.d(TAG, "onCreate:::: my_preference and mCurrentValue=" + mCurrentValue);
        if (mCurrentValue) {
            setTheme(R.style.MyDarkTheme);
            Log.d(TAG, "onCreate:::: setTheme:MyDarkTheme");
        } else {
            setTheme(R.style.MyLightTheme);
            Log.d(TAG, "onCreate:::: setTheme:MyLightTheme");
        }

        super.onCreate(savedInstanceState);

        setContentView(R.layout.my_activity);
    }

    private int tabIndex = 0;

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("tabIndex", tabIndex);
    }

    // in order to recreate Activity we must check value here because when we come back from another Activity onCreate doesn't called again
    @Override
    protected void onStart() {
        super.onStart();
        if (activityRecreated) {
            MyFragment newFragment = new MyFragment();
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.detail_container, newFragment);
            transaction.commit();
        }

        setContentView(R.layout.my_activity);

        SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        Boolean mNewValue = mSharedPreferences.getBoolean("my_preference", false);
        // if value differs from previous Theme, we recreate Activity
        if(mCurrentValue != mNewValue){
            recreate();
        }           
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            final Intent intent = getParentActivityIntent();
            if(intent != null){
                intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            }
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

片段

public class MyFragment extends android.support.v4.app.Fragment {

    public MyFragment() {}

    ViewPager pager;
    MyViewPagerAdapter adapter;
    SlidingTabLayout_LightTabStrip stationtabs;
    int Numboftabs = 5;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.my_fragment, container, false);
        super.onCreate(savedInstanceState);

        return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        View v = getView();

        adapter =  new MyViewPagerAdapter(getChildFragmentManager(), Numboftabs, getActivity());

        // assigning ViewPager View and setting the adapter
        assert v != null;
        pager = v.findViewById(R.id.pager);
        pager.setAdapter(adapter);

        // assigning the Sliding Tab Layout view
        mytabs = v.findViewById(R.id.productTabs);
        mytabs.setDistributeEvenly(false);

        mytabs.setViewPager(pager);

        super.onActivityCreated(savedInstanceState);
    }

    private class MyViewPagerAdapter extends FragmentStatePagerAdapter {

        private int NumbOfTabs;
        private Context context;

        MyViewPagerAdapter(FragmentManager fm, int mNumbOfTabsumb, Context context) {
            super(fm);

            this.context = context;
            this.NumbOfTabs = mNumbOfTabsumb;
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return new FragmentProductA();
                case 1:
                    return new FragmentProductB();
                case 2:
                    return new FragmentProductC();
                case 3:
                    return new FragmentProductD();
                case 4:
                    return new FragmentProductE();
            }
            return null;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return context.getResources().getString(R.string.product_a);
                case 1:
                    return context.getResources().getString(R.string.product_b);
                case 2:
                    return context.getResources().getString(R.string.product_c);
                case 3:
                    return context.getResources().getString(R.string.product_d);
                case 4:
                    return context.getResources().getString(R.string.product_e);
            }
            return super.getPageTitle(position);
        }

        @Override
        public int getCount() {
            return NumbOfTabs;
        }
    }
}

0 个答案:

没有答案