我有一个包含几个 SherlockFragmentActivities 和 SherlockFragment 的应用程序。我无法支持活动之间的轮换和过渡。
在我的第一个 SherlockFragmentActivity 中,我尝试使用以下代码捕捉旋转:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
InitializeUI();
details = new MeetingConnectFragment();
getSupportFragmentManager().beginTransaction().add(
R.id.activity_scheduled_meeting_connect, details).commit();
}
}
//this is called when the screen rotates.
// (onCreate is no longer called when screen rotates due to manifest, see: android:configChanges)
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
InitializeUI();
details = new MeetingConnectFragment();
details.setInfo(cInfo);
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
trans.replace(R.id.activity_scheduled_meeting_details, details);
trans.addToBackStack(null);
trans.commit();
}
private void InitializeUI() {
setContentView(R.layout.activity_meeting_connect);
// other stuff cut for brevity
}
在 MeetinConnectFragment 中有一个按钮,可以调出不同的活动。如果用户在第二个活动中旋转屏幕,然后返回到这个活动,程序会崩溃并出现以下异常:
E/AndroidRuntime(17976): java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
E/AndroidRuntime(17976): at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1299)
E/AndroidRuntime(17976): at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1310)
E/AndroidRuntime(17976): at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:541)
E/AndroidRuntime(17976): at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:525)
E/AndroidRuntime(17976): at com.level3.connect.meetings.MeetingConnectActivity.onConfigurationChanged(MeetingConnectActivity.java:49)
E/AndroidRuntime(17976): at android.app.ActivityThread.performConfigurationChanged(ActivityThread.java:3675)
E/AndroidRuntime(17976): at android.app.ActivityThread.handleActivityConfigurationChanged(ActivityThread.java:3849)
E/AndroidRuntime(17976): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1370)
E/AndroidRuntime(17976): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(17976): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(17976): at android.app.ActivityThread.main(ActivityThread.java:4921)
E/AndroidRuntime(17976): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(17976): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(17976): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
E/AndroidRuntime(17976): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
E/AndroidRuntime(17976): at dalvik.system.NativeStart.main(Native Method)
例外是在线提交更改。我一直在试图弄清楚如何处理这种情况,但并不完全在那里。帮助表示赞赏。