我有一个一次显示一个片段的活动,现在每个片段都从服务器加载数据并继续在列表中获取数据,用户可以通过选择显示在 ActonBar 中的 Spinner 中的项目来在活动中显示另一个片段。
这是活动的代码
public class HomeActivity extends SherlockFragmentActivity .....{
private class ListInfo {
private String tag;
private Class<?> clss;
private Bundle bundle;
private Fragment fragment;
public ListInfo(String tag, Class<?> clss, Bundle bundle) {
this.tag = tag;
this.clss = clss;
this.bundle = bundle;
}
}
String[] naviStrings;
private HashMap<String, ListInfo> listMap = new HashMap<String, HomeActivity.ListInfo>();
private ListInfo mLastListInfo = null;
private int currentSelectedOptionInSpinner;
ChannelFragment fr;
@SuppressWarnings("unchecked")
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
Log.d("HomeActivity", "onCreate");
Log.d("onCreate bundle", "" + arg0);
setContentView(R.layout.test);
naviStrings = getResources().getStringArray(
R.array.action_bar_spinner_entries);
initializeList(arg0);
Context context = getSupportActionBar().getThemedContext();
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
context, R.array.action_bar_spinner_entries,
R.layout.sherlock_spinner_item);
adapter.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getSupportActionBar().setListNavigationCallbacks(adapter, this);
getSupportActionBar().setSelectedNavigationItem(
currentSelectedOptionInSpinner);
}
private void initializeList(Bundle args) {
ListInfo listInfo = null;
// add first Fragment
listMap.put(naviStrings[5], (listInfo = new ListInfo(naviStrings[5],
AboutUsActivity.class, args)));
addFragmentToList(this, listInfo);
listMap.put(naviStrings[3], (listInfo = new ListInfo(naviStrings[3],
Fragment1.class, args)));
addFragmentToList(this, listInfo);
listMap.put(naviStrings[2], (listInfo = new ListInfo(naviStrings[2],
Fragment2.class, args)));
addFragmentToList(this, listInfo);
listMap.put(naviStrings[1], (listInfo = new ListInfo(naviStrings[1],
Fragment3.class, args)));
addFragmentToList(this, listInfo);
listMap.put(naviStrings[4], (listInfo = new ListInfo(naviStrings[4],
Fragment4.class, args)));
addFragmentToList(this, listInfo);
if (args != null) {
Toast.makeText(this, "args is not null", Toast.LENGTH_SHORT).show();
// set the current selected index in the ActionBar spinner
if (args.getInt(
ApplicationMetaData.IntentData.LAST_SELECTED_ITEM_SPINNER,
-1) > -1
&& args.getInt(
ApplicationMetaData.IntentData.LAST_SELECTED_ITEM_SPINNER,
-1) < naviStrings.length) {
Toast.makeText(this, "selected is not null",
Toast.LENGTH_SHORT).show();
currentSelectedOptionInSpinner = args
.getInt(ApplicationMetaData.IntentData.LAST_SELECTED_ITEM_SPINNER);
} else {
currentSelectedOptionInSpinner = 2;
}
} else {
currentSelectedOptionInSpinner = 2;
}
onNavigationItemSelected(currentSelectedOptionInSpinner, 0);
}
private static void addFragmentToList(SherlockFragmentActivity activity,
ListInfo instanse) {
// check to see if we already have a fragment for this tab , probably
// from a previously save state.
// if so deactivated it ,because our initial state is that a tab is not
// shown.
String tag = instanse.tag;
instanse.fragment = activity.getSupportFragmentManager()
.findFragmentByTag(tag);
if (instanse.fragment != null && !instanse.fragment.isDetached()) {
FragmentTransaction ft = activity.getSupportFragmentManager()
.beginTransaction();
ft.detach(instanse.fragment);
ft.commit();
activity.getSupportFragmentManager().executePendingTransactions();
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(
ApplicationMetaData.IntentData.LAST_SELECTED_ITEM_SPINNER,
currentSelectedOptionInSpinner);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
Log.d("onNavigationItemSelected", "" + itemPosition);
if (itemPosition == 5) {
Intent i = new Intent(this, AboutUsActivity.class);
startActivity(i);
return true;
}
Log.d("fragment data", listMap.get(naviStrings[itemPosition]) + "");
ListInfo newtItem = listMap.get(naviStrings[itemPosition]);
if (newtItem != mLastListInfo) {
FragmentTransaction ft = this.getSupportFragmentManager()
.beginTransaction();
if (mLastListInfo != null) {
if (mLastListInfo.fragment != null) {
ft.detach(mLastListInfo.fragment);
}
}
if (newtItem != null) {
if (newtItem.fragment == null) {
// create and add
newtItem.fragment = Fragment.instantiate(this,
newtItem.clss.getName(), newtItem.bundle);
ft.add(android.R.id.content, newtItem.fragment,
newtItem.tag);
} else {
ft.attach(newtItem.fragment);
}
}
mLastListInfo = newtItem;
ft.commit();
this.getSupportFragmentManager().executePendingTransactions();
return true;
}
return false;
}}
现在,当我从一个片段导航到另一个片段时,可以说从片段 1 到片段 2,当我返回片段 1 时,它会保留它的状态并且不必从头开始加载数据,但是如果我从主 Activity 启动一个新的 Activity 系统销毁 Activity 和其中的 Fragments,有没有办法在我旋转 Home Activity 时保留这些 Fragment 注释,没有发生任何事情,只有当我开始一个新的 Activity 时??????
更新
我在onCreate
setRetaineInstance(true) 中制作的所有四个片段;