我里面有多个片段MainActivity
我只想在方向更改时配置DetailFragment
public class DetailFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
WindowManager wm = getActivity().getWindowManager();
Display d = wm.getDefaultDisplay();
if(d.getWidth() > d.getHeight()){
mDetailView = inflater.inflate(R.layout.detail, container, false);
}else{
mDetailView = inflater.inflate(R.layout.detail_port, container, false);
}
gridView = (GridView)mDetailView.findViewById(R.id.grid);
return mDetailView;
}
}
所以,我必须onConfigurationChanged
在SubTopicFragment
下面重写是SubTopicFragment extend ListFragment
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
updateDetial(topic_index, args.getString("group_id"), args.getString("group_name"));
}
public void updateDetial(int subTopicPosition,String groupID, String groupName){
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
DetailFragment detailView = (DetailFragment)getFragmentManager()
.findFragmentById(R.id.detailFrame);
DetailFragment detailFrag = new DetailFragment(topic_index, subTopicPosition, groupID, groupName);
if(detailView==null){
ft.add(R.id.detailFrame, detailFrag);
ft.commit();
}else{
ft.replace(R.id.detailFrame, detailFrag);
ft.commit();
}
}
它运作良好。但是当我移动到另一个活动时出现问题,改变方向并按下返回按钮,它会导致强制关闭我该如何解决这个问题?
06-13 10:15:54.758: E/AndroidRuntime(1050): FATAL EXCEPTION: main
06-13 10:15:54.758: E/AndroidRuntime(1050): java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
06-13 10:15:54.758: E/AndroidRuntime(1050): at android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1192)
06-13 10:15:54.758: E/AndroidRuntime(1050): at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1203)
06-13 10:15:54.758: E/AndroidRuntime(1050): at android.app.BackStackRecord.commitInternal(BackStackRecord.java:557)
06-13 10:15:54.758: E/AndroidRuntime(1050): at android.app.BackStackRecord.commit(BackStackRecord.java:541)
06-13 10:15:54.758: E/AndroidRuntime(1050): at com.april.android.SubTopicFragment.updateDetial(SubTopicFragment.java:120)
06-13 10:15:54.758: E/AndroidRuntime(1050): at com.april.android.SubTopicFragment.onConfigurationChanged(SubTopicFragment.java:259)
06-13 10:15:54.758: E/AndroidRuntime(1050): at android.app.FragmentManagerImpl.dispatchConfigurationChanged(FragmentManager.java:1706)
06-13 10:15:54.758: E/AndroidRuntime(1050): at android.app.Activity.onConfigurationChanged(Activity.java:1402)
06-13 10:15:54.758: E/AndroidRuntime(1050): at android.app.ActivityThread.performConfigurationChanged(ActivityThread.java:3298)
06-13 10:15:54.758: E/AndroidRuntime(1050): at android.app.ActivityThread.handleActivityConfigurationChanged(ActivityThread.java:3427)
06-13 10:15:54.758: E/AndroidRuntime(1050): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1113)
06-13 10:15:54.758: E/AndroidRuntime(1050): at android.os.Handler.dispatchMessage(Handler.java:99)
06-13 10:15:54.758: E/AndroidRuntime(1050): at android.os.Looper.loop(Looper.java:132)
06-13 10:15:54.758: E/AndroidRuntime(1050): at android.app.ActivityThread.main(ActivityThread.java:4123)
06-13 10:15:54.758: E/AndroidRuntime(1050): at java.lang.reflect.Method.invokeNative(Native Method)
06-13 10:15:54.758: E/AndroidRuntime(1050): at java.lang.reflect.Method.invoke(Method.java:491)
06-13 10:15:54.758: E/AndroidRuntime(1050): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
06-13 10:15:54.758: E/AndroidRuntime(1050): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
06-13 10:15:54.758: E/AndroidRuntime(1050): at dalvik.system.NativeStart.main(Native Method)