0

我正在使用 Jake Wharton 的 ViewPagerIndicator 库,在下面显示的 FragmentActivity 中,当我旋转屏幕时,调用了 onCreate 方法,但活动保持空白,仅显示指示器(“Tab 1”和“Tab 2”)。

public class MainActivity extends FragmentActivity {

    private static final String[] CONTENT = new String[] { "Tab 1", "Tab 2", };

    ProgressDialog progress;

    FragmentPagerAdapter adapter;
    ViewPager pager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        progress = ProgressDialog.show(this, "Loading", "Loading data...");

        new getData().execute();

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {

        super.onSaveInstanceState(outState);

    }

    class FragPagerAdapter extends FragmentPagerAdapter {
        public FragPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if(position == 0){
                return FragmentTabOne.newInstance();
            }else if(position == 1){
                return FragmentTabTwo.newInstance();
            }
            return null;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return CONTENT[position % CONTENT.length].toUpperCase();
        }

        @Override
        public int getCount() {
            return CONTENT.length;
        }

    }

    private class getData extends AsyncTask<Void, Void, Boolean> {

        @Override
        protected Boolean doInBackground(Void... params) {

            //getting data from server

            return true;
        }

        protected void onPostExecute(final Boolean success) {

                setContentView(R.layout.main_activity); //indicator + viewpager

                adapter = new FragPagerAdapter(getSupportFragmentManager());

                pager = (ViewPager)findViewById(R.id.pager);

                pager.setAdapter(adapter);

                TabPageIndicator indicator = (TabPageIndicator)findViewById(R.id.indicator);
                indicator.setViewPager(pager);

                progress.dismiss();

            }
        }

    }
}

我应该怎么做(可能在 onSaveInstanceState 方法中)才能正确显示活动及其片段?谢谢

4

1 回答 1

1

在您的片段中onCreateView()尝试添加对setRetainInstance(true)

于 2013-05-17T21:18:28.593 回答