我有一个问题,涉及一个使用目标SDK 4.3编译和运行的Android应用程序。该应用程序有两个活动(Activity),一个MainActivity,也是启动器活动(Launcher Activity),另一个是SecondActivity。这两个活动都使用片段(Fragments)。为了支持旧设备,使用了支持库(support lib)。
在以下情况下,会出现“IllegalStateException: Fragment already added”错误:
1)启动应用程序->显示MainActivity
2)通过意图(Intent)切换到SecondActivity
3)按下主页按钮(Home Button)
4)等待较长时间(测试了24小时)
5)再次按下应用程序图标->异常。如果时间较短,则像预期那样显示SecondActivity。
我已经阅读了很多有关处理Fragment时的IllegalStateExceptions,但是所有指向replace()方法的问题。在堆栈跟踪中,我的自己的代码从未被调用。
片段(Fragment)在活动(Activity)的onCreate()方法内添加:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(..);
ListFragment listFragment = this.getCaptureListFragment();
FragmentTransaction tx = this.getSupportFragmentManager().beginTransaction();
tx.add(R.id.MainFragmentContainer, listFragment, "list_fragment_tag");
tx.commit();
}
private ListFragment getListFragment() {
ListFragment listFragment = (ListFragment) this.getSupportFragmentManager().findFragmentByTag("list_fragment_tag");
if (listFragment == null) {
listFragment = new ListFragment();
}
return listFragment;
}
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.myexample.demo/de.myexample.demo.ui.SecondActivity}: java.lang.IllegalStateException: Fragment already added: ListFragment{42283f58 #0 id=0x7f060094 de.myexample.demo.ui.ListFragment}
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: Fragment already added: ListFragment{42283f58 #0 id=0x7f060094 de.myexample.demo.ui.ListFragment}
at android.support.v4.app.FragmentManagerImpl.addFragment(SourceFile:1175)
at android.support.v4.app.BackStackRecord.run(SourceFile:616)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(SourceFile:1460)
at android.support.v4.app.FragmentActivity.onStart(SourceFile:556)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
at android.app.Activity.performStart(Activity.java:5143)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
... 11 more
java.lang.IllegalStateException: Fragment already added: ListFragment{42283f58 #0 id=0x7f060094 de.myexample.demo.ui.ListFragment}
at android.support.v4.app.FragmentManagerImpl.addFragment(SourceFile:1175)
at android.support.v4.app.BackStackRecord.run(SourceFile:616)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(SourceFile:1460)
at android.support.v4.app.FragmentActivity.onStart(SourceFile:556)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
at android.app.Activity.performStart(Activity.java:5143)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)