我有一个主要活动,其中有actionbarsherlok标签。这些标签内放置了三个片段。按下主页按钮后,我立即从后台恢复应用程序,所有片段都显示没有任何问题。但是,当我尝试恢复后时间,碎片消失了。我只能看到标题页。为了澄清,我提供了主要活动的代码,
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_main);
ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Drawable d=getResources().getDrawable(R.drawable.tab_bg);
// bar.setBackgroundDrawable(d);
ActionBar.Tab tab1 = bar.newTab();
ActionBar.Tab tab2 = bar.newTab();
ActionBar.Tab tab3 = bar.newTab();
tab1.setText("Holidays");
// tab1.setIcon(R.drawable.abs__ic_menu_share_holo_dark);
tab2.setText("Traveller's Tales");
// tab2.setIcon(R.drawable.abs__ic_voice_search);
tab3.setText("Moments");
// tab3.setIcon(R.drawable.abs__ic_cab_done_holo_dark);
tab1.setTabListener(new MyTabListener());
tab2.setTabListener(new MyTabListener());
tab3.setTabListener(new MyTabListener());
bar.addTab(tab1);
bar.addTab(tab2);
bar.addTab(tab3);
if (Utils.isNetworkAvailable(MainActivity.this)) {
new GetMainCategory(MainActivity.this).execute();
}
else {
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
.create();
alertDialog.setMessage("No network connection!");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Add your code for the button here.
finish();
}
});
alertDialog.show();
}
}
private class MyTabListener implements ActionBar.TabListener {
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
switch (tab.getPosition()) {
case 0:
if (frag1 == null) {
// If not, instantiate and add it to the activity
frag1 = Fragment.instantiate(getApplicationContext(),
PackagesFragment.class.getName());
ft.add(android.R.id.content, frag1, "Feeds");
} else {
// If it exists, simply attach it in order to show it
ft.show(frag1);
}
return;
case 1:
if (frag2 == null) {
// If not, instantiate and add it to the activity
frag2 = Fragment.instantiate(getApplicationContext(),
BlogsFragment.class.getName());
ft.add(android.R.id.content, frag2, "Profile");
} else {
// If it exists, simply attach it in order to show it
ft.show(frag2);
}
return;
case 2:
if (frag3 == null) {
// If not, instantiate and add it to the activity
frag3 = Fragment.instantiate(getApplicationContext(),
GalleryFragment.class.getName());
ft.add(android.R.id.content, frag3, "Gallery");
} else {
// If it exists, simply attach it in order to show it
ft.show(frag3);
}
return;
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
// Detach the fragment, because another one is being attached
switch (tab.getPosition()) {
case 0:
ft.hide(frag1);
return;
case 1:
ft.hide(frag2);
return;
case 2:
ft.hide(frag3);
return;
}
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
//Here I have an async task.
}