我有一个带有2个标签的应用程序,其中添加了fragments
。所以这就是层次结构:
MainActivity扩展了Activity:
TabHosts
用于添加2个标签..每个标签都展开FragmentActivity
fragmentManager
用于在每个标签中添加一个片段..每个片段都展开Fragment
我有overriden
所有onstop
方法,并在调用它们时记录。以防我在调用它时需要allowstatelosscommit
。
问题:
如果我没有在选项卡之间切换回第四个,应用程序就可以正常关闭。但是当我做一些导航时,我最终得到了这个例外:
05-29 15:50:02.255: E/AndroidRuntime(12342): FATAL EXCEPTION: main
05-29 15:50:02.255: E/AndroidRuntime(12342): java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
05-29 15:50:02.255: E/AndroidRuntime(12342): at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1343)
05-29 15:50:02.255: E/AndroidRuntime(12342): at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:486)
05-29 15:50:02.255: E/AndroidRuntime(12342): at android.support.v4.app.FragmentActivity.onBackPressed(FragmentActivity.java:179)
05-29 15:50:02.255: E/AndroidRuntime(12342): at orient.video.tabs.TabTVShows.onBackPressed(TabTVShows.java:222)
05-29 15:50:02.255: E/AndroidRuntime(12342): at android.app.Activity.onKeyUp(Activity.java:2099)
05-29 15:50:02.255: E/AndroidRuntime(12342): at android.view.KeyEvent.dispatch(KeyEvent.java:3200)
05-29 15:50:02.255: E/AndroidRuntime(12342): at android.app.Activity.dispatchKeyEvent(Activity.java:2329)
05-29 15:50:02.255: E/AndroidRuntime(12342): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1806)
05-29 15:50:02.255: E/AndroidRuntime(12342): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
05-29 15:50:02.255: E/AndroidRuntime(12342): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
05-29 15:50:02.255: E/AndroidRuntime(12342): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
05-29 15:50:02.255: E/AndroidRuntime(12342): at android.widget.TabHost.dispatchKeyEvent(TabHost.java:298)
05-29 15:50:02.255: E/AndroidRuntime(12342): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
05-29 15:50:02.255: E/AndroidRuntime(12342): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
05-29 15:50:02.255: E/AndroidRuntime(12342): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
05-29 15:50:02.255: E/AndroidRuntime(12342): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1246)
05-29 15:50:02.255: E/AndroidRuntime(12342): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1879)
05-29 15:50:02.255: E/AndroidRuntime(12342): at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1361)
05-29 15:50:02.255: E/AndroidRuntime(12342): at android.app.Activity.dispatchKeyEvent(Activity.java:2324)
05-29 15:50:02.255: E/AndroidRuntime(12342): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1806)
05-29 15:50:02.255: E/AndroidRuntime(12342): at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3327)
05-29 15:50:02.255: E/AndroidRuntime(12342): at android.view.ViewRootImpl.handleFinishedEvent(ViewRootImpl.java:3300)
05-29 15:50:02.255: E/AndroidRuntime(12342): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2460)
05-29 15:50:02.255: E/AndroidRuntime(12342): at android.os.Handler.dispatchMessage(Handler.java:99)
05-29 15:50:02.255: E/AndroidRuntime(12342): at android.os.Looper.loop(Looper.java:137)
05-29 15:50:02.255: E/AndroidRuntime(12342): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-29 15:50:02.255: E/AndroidRuntime(12342): at java.lang.reflect.Method.invokeNative(Native Method)
05-29 15:50:02.255: E/AndroidRuntime(12342): at java.lang.reflect.Method.invoke(Method.java:511)
05-29 15:50:02.255: E/AndroidRuntime(12342): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:848)
05-29 15:50:02.255: E/AndroidRuntime(12342): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)
05-29 15:50:02.255: E/AndroidRuntime(12342): at dalvik.system.NativeStart.main(Native Method)
由于状态正在搞乱,我希望在应用程序关闭时从fragmentmanager
明确删除片段。我将onbackpressed
挂钩在其中一个标签中:
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
Log.v("tab tv", "back pressed");
FragmentManager fm = getSupportFragmentManager();
Log.v("frag backstack", String.valueOf(fm.getBackStackEntryCount()));
super.onBackPressed();
}
但backstackentrycount
在我的情况下为0 ..当我发生这种情况时,没有tasks
正在运行..我在这里做错了什么??
修改
以下是我在OnCreate
中添加标签的方法:
tabHost = (TabHost) findViewById(android.R.id.tabhost);
LocalActivityManager mLocalActivityManager = new LocalActivityManager(this, false);
mLocalActivityManager.dispatchCreate(savedInstanceState);
tabHost.setup(mLocalActivityManager);
TabSpec tab1 = tabHost.newTabSpec("TV Shows");
TabSpec tab2 = tabHost.newTabSpec("Movies");
tab1.setIndicator("TV Shows");
tab1.setContent(new Intent(this, TabTVShows.class));
tab2.setIndicator("Movies");
tab2.setContent(new Intent(this, TabMovies.class));
tabHost.addTab(tab1);
tabHost.addTab(tab2);
以下是用于在标签中设置片段的PageAdapter
:
public class VideosPageAdapter extends FragmentPagerAdapter implements IconPagerAdapter {
private List<Fragment> fragments;
public VideosPageAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
@Override
public int getCount() {
return this.fragments.size();
}
@Override
public int getIconResId(int index) {
// TODO Auto-generated method stub
return 0;
}
}
这是我的标签代码:
public class TabMovies extends FragmentActivity implements OnPageChangeListener {
public static void notifyAdapter(List<Movie> movies)
{
numOfPages = (int)Math.ceil(movies.size()/(float)MainActivity.totalItems);
List<Fragment> fragments = getFragments(numOfPages);
pageAdapter = new VideosPageAdapter(fm, fragments);
mViewPager.setAdapter(pageAdapter);
pageAdapter.notifyDataSetChanged();
}
private static List<Fragment> getFragments(int numOfPages) {
List<Fragment> fList = new ArrayList<Fragment>();
FragmentMoviesMain[] f = new FragmentMoviesMain[numOfPages];
for (int i = 0; i < numOfPages; i++) {
f[i] = new FragmentMoviesMain().newInstance(String
.valueOf(fragmentCounter));
fragmentCounter++;
fList.add(f[i]);
}
return fList;
}