0
我使用ViewPagerIndicator庫,由Jake Wharton在下面顯示的FragmentActivity中旋轉屏幕時調用onCreate方法,但活動保持空白並僅顯示指標('Tab 1'和'Tab 2' )。在屏幕旋轉後恢復FragmentActivity
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方法中)活動及其片段要正確顯示?謝謝