android.util.AndroidRuntimeException: requestFeature() must be called before adding content - java

I am getting this android.util.AndroidRuntimeException: requestFeature() must be called before adding content error. As you can see in the below code, the requestWindowFeature(Window.FEATURE_NO_TITLE); line comes before setContentView(R.layout.mainmenu); line of code. This onCreate() code is the same format in just about every one of my activities and I've never had trouble with it before until now. Ever since I updated to ADT 22 a lot of random errors have been popping up everywhere. I have weeded through a lot of those errors and this is my latest one.
What can I do to fix this error?
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.mainmenu);
LogCat
05-31 04:20:43.121: E/AndroidRuntime(14559): FATAL EXCEPTION: main
05-31 04:20:43.121: E/AndroidRuntime(14559): java.lang.RuntimeException: Unable to start activity ComponentInfo{matt.lyons.bibletrivia.lite/matt.lyons.bibletrivia.lite.MainMenu}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
05-31 04:20:43.121: E/AndroidRuntime(14559): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-31 04:20:43.121: E/AndroidRuntime(14559): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-31 04:20:43.121: E/AndroidRuntime(14559): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-31 04:20:43.121: E/AndroidRuntime(14559): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-31 04:20:43.121: E/AndroidRuntime(14559): at android.os.Handler.dispatchMessage(Handler.java:99)
05-31 04:20:43.121: E/AndroidRuntime(14559): at android.os.Looper.loop(Looper.java:137)
05-31 04:20:43.121: E/AndroidRuntime(14559): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-31 04:20:43.121: E/AndroidRuntime(14559): at java.lang.reflect.Method.invokeNative(Native Method)
05-31 04:20:43.121: E/AndroidRuntime(14559): at java.lang.reflect.Method.invoke(Method.java:511)
05-31 04:20:43.121: E/AndroidRuntime(14559): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-31 04:20:43.121: E/AndroidRuntime(14559): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-31 04:20:43.121: E/AndroidRuntime(14559): at dalvik.system.NativeStart.main(Native Method)
05-31 04:20:43.121: E/AndroidRuntime(14559): Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
05-31 04:20:43.121: E/AndroidRuntime(14559): at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:229)
05-31 04:20:43.121: E/AndroidRuntime(14559): at android.app.Activity.requestWindowFeature(Activity.java:3244)
05-31 04:20:43.121: E/AndroidRuntime(14559): at matt.lyons.bibletrivia.lite.MainMenu.onCreate(MainMenu.java:28)
05-31 04:20:43.121: E/AndroidRuntime(14559): at android.app.Activity.performCreate(Activity.java:5104)
05-31 04:20:43.121: E/AndroidRuntime(14559): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-31 04:20:43.121: E/AndroidRuntime(14559): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-31 04:20:43.121: E/AndroidRuntime(14559): ... 11 more

I also faced this problem but when i call window request before calling super.onCreate() then problem was solved, please try it also like..
#Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenu);
}
Hope this will help you...:)
Edited: For other possible solutions for Android'd new versions
Hide the Status Bar on Android 4.0 and Lower
<application
...
android:theme="#android:style/Theme.Holo.NoActionBar.Fullscreen" >
...
</application>
The advantages of using an activity theme are as follows:
It's easier to maintain and less error-prone than setting a flag programmatically.
It results in smoother UI transitions, because the system has the information it needs to render your UI before instantiating your app's main activity.
Android version is lower than Jellybean
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// If the Android version is lower than Jellybean, use this call to hide
// the status bar.
if (Build.VERSION.SDK_INT < 16) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
setContentView(R.layout.activity_main);
}
Hide the Status Bar on Android 4.1 and Higher
View decorView = getWindow().getDecorView();
// Hide the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
// Remember that you should never show the action bar if the
// status bar is hidden, so hide that too if necessary.
ActionBar actionBar = getActionBar();
actionBar.hide();
Note the following:
Once UI flags have been cleared (for example, by navigating away from the activity), your app needs to reset them if you want to hide the bars again. See Responding to UI Visibility Changes for a discussion of how to listen for UI visibility changes so that your app can respond accordingly.
Where you set the UI flags makes a difference. If you hide the system bars in your activity's onCreate() method and the user presses Home, the system bars will reappear. When the user reopens the activity, onCreate() won't get called, so the system bars will remain visible. If you want system UI changes to persist as the user navigates in and out of your activity, set UI flags in onResume() or onWindowFocusChanged().
The method setSystemUiVisibility() only has an effect if the view you call it from is visible.
Navigating away from the view causes flags set with setSystemUiVisibility() to be cleared.

I got that exception (android.util.AndroidRuntimeException: requestFeature() must be called before adding content) when using
requestWindowFeature(Window.FEATURE_NO_TITLE);
in an older device running Android 2.3.5 (Gingerbread). I am using the v7 support library.
The error was fixed when I changed it to use:
supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
(This comes after my super.onCreate call in the fix, too). See docs at https://developer.android.com/reference/android/support/v7/app/ActionBarActivity.html#supportRequestWindowFeature(int)
So it may be more a case of a misleading error message than anything else.

Please check your class is extend from Activity or ActionBarActivity. If you are using ActionBarActivity please use Activity.

If you're using your activity as a Dialog (with Theme.Dialog), then make sure you extend Activity instead of ActionBarActivity (the default given to you with the Android Studio wizard). Then you can use
requestWindowFeature(Window.FEATURE_NO_TITLE);
or
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
AFTER the super.onCreate()...

I also ran into this error from a different workflow. I created a custom DialogFragment class and I created two #Override functions - onCreateView and onCreateDialog. My onCreateView function grabbed a custom layout for the fragment, and my onCreateDialog function created an AlertDialog.Builder.
This appeared to not work because the onCreateDialog is called before onCreateView. After I deleted onCreateView [by moving my custom view inflation into onCreateDialog, i encountered the error:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
I realized my difficulty came from trying to implement both overrides since I wanted to 1) use a layout for the main view of the dialog, and 2) use the Builder pre-defined Positive / Negative buttons. My solution was to create the positive / negative buttons in my custom dialog view, so i deleted my implementation of the Override onCreateDialog function.
Hope this helps someone in the future!
Here are some SO questions that helped me:
Problem inflating custom view for AlertDialog in DialogFragment
ViewPager in DialogFragment - IllegalStateException: Fragment does not have a view
Custom Layout for DialogFragment OnCreateView vs. OnCreateDialog
The specified child already has a parent. You must call removeView() on the child's parent first

I was extending a DialogFragment and the above answer didnot work. I had to use getDialog() to remove the title:
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:windowActionBar">false</item>
</style>
just only set style like this no any coding side changing is required.

Extend (Activity) instead of (ActionBarActivity)
example: public class Itemdetails extends Activity {....
then in the onCreate write:
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.Your_Activity);

i think the simplest way to do this is use this below code
getActionBar().setTitle(null);

Please try the following options, at least one of these should work for you:
If you are using a DialogFragment then try not overriding both methods onCreateDialog(..) and onCreateView(..) (This change worked for me)
try supportRequestWindowFeature(Window.FEATURE_NO_TITLE)/requestWindowFeature(Window.FEATURE_NO_TITLE) before setContentView(..) in the activity and after super.onCreate(..);
Try option 2 before super.onCreate(..);

Misplaced your line:
super.onCreate(savedInstanceState);
use this Manner:
#Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qrscanner);

I had this error with AlertDialog and the error was happening in API 19 only when dialog.show() was called.
figured out that the android.app.AlertDialog import is causing the problem so I changed it to androidx.appcompat.app.AlertDialog and it fixed it.
Note that this will work if you are using androidx. if you are not, you should use the old appcompat version of that import.

Related

Leaked IntentReceiver Exception using osmdroid

I have in my Android app a MapActivity, that shows map using osmdroid (Open Street Map for Android library).
When I'm in this MapActivity and I go to another activity, I get always an exception, and I cannot understand how to avoid it, because I think that it is related to something done inside the library.
As you can see in the following log, it happen after destroying of MapActivity
05-18 17:34:31.117 27073-27073/com.fpricoco.etip I/﹕ Classe 'MapActivity' ---- 'onDestroy()' ---- Stato: Distrutta
05-18 17:34:31.157 27073-27073/com.fpricoco.etip E/ActivityThread﹕ Activity com.fpricoco.etip.Activities.MapActivity has leaked IntentReceiver org.osmdroid.tileprovider.modules.MapTileFileStorageProviderBase$MyBroadcastReceiver#43d85f60 that was originally registered here. Are you missing a call to unregisterReceiver()?
android.app.IntentReceiverLeaked: Activity com.fpricoco.etip.Activities.MapActivity has leaked IntentReceiver org.osmdroid.tileprovider.modules.MapTileFileStorageProviderBase$MyBroadcastReceiver#43d85f60 that was originally registered here. Are you missing a call to unregisterReceiver()?
at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:792)
at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:593)
at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1151)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:1138)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:1132)
at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:365)
at org.osmdroid.tileprovider.util.SimpleRegisterReceiver.registerReceiver(SimpleRegisterReceiver.java:21)
at org.osmdroid.tileprovider.modules.MapTileFileStorageProviderBase.<init>(MapTileFileStorageProviderBase.java:34)
at org.osmdroid.tileprovider.modules.MapTileFileArchiveProvider.<init>(MapTileFileArchiveProvider.java:57)
at org.osmdroid.tileprovider.modules.MapTileFileArchiveProvider.<init>(MapTileFileArchiveProvider.java:76)
at org.osmdroid.tileprovider.MapTileProviderBasic.<init>(MapTileProviderBasic.java:63)
at org.osmdroid.tileprovider.MapTileProviderBasic.<init>(MapTileProviderBasic.java:41)
at org.osmdroid.tileprovider.MapTileProviderBasic.<init>(MapTileProviderBasic.java:34)
at org.osmdroid.views.overlay.MinimapOverlay.<init>(MinimapOverlay.java:114)
at com.fpricoco.etip.Activities.MapActivity.onCreate(MapActivity.java:303)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2034)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2095)
at android.app.ActivityThread.access$600(ActivityThread.java:137)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:4791)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
at dalvik.system.NativeStart.main(Native Method)
Searching on Google I found that I have to "unregisterReceiver(yourReceiver)", the point is that I don't have in my case any "yourReceiver" setted in my code, I think it is done inside library.
I also found this solution "https://groups.google.com/forum/#!topic/osmdroid/wDEvFpWfTIU" , but I don't understand how to detach
Finally I also tried in this way, but it didn't work:
// .. get custom tiles off the screen
myMapView.getOverlays().remove(mMinimapOverlay);
// .. release all custom tiles' bitmaps
myMapView.getTileProvider().clearTileCache();
// .. unregister intent receiver
myMapView.getTileProvider().detach();
The library I imported is:
compile 'org.osmdroid:osmdroid-android:5.1#aar'
inside my gradle.build file.
try this:
#Override
public void onDestroy() {
myMapView.onDetach();
myMapView.getTileProvider().clearTileCache();
}

NullPointerException in logcat with no clues

I am violating best practices here trying to figure this out, so to start off with, I realize that and I appreciate any insight. I'm making something simple that relies on a lot of Dialog calls. Nothing fancy, not calling a lot of Fragments. My game will require no permissions.
With the backstory out of the way, I'm getting this NPE:
06-13 01:29:03.581 24690-24690/com.myapp.myapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.myapp.myapp, PID: 24690
java.lang.NullPointerException
at android.widget.FrameLayout.onMeasure(FrameLayout.java:309)
at android.view.View.measure(View.java:17388)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5353)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:17388)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5353)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2533)
at android.view.View.measure(View.java:17388)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2217)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1354)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1553)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1238)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6473)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
at android.view.Choreographer.doCallbacks(Choreographer.java:603)
at android.view.Choreographer.doFrame(Choreographer.java:573)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
In my very flat app, I do this:
public static MainActivity ACTIVITY;
#Override
protected void onCreate(Bundle savedInstanceState) {
ACTIVITY = this;
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
ACTIVITY.setContentView(R.layout.activity_main);
Then I do a bunch of stuff that works fine. The problem is, I call a class that does a battle event:
FightEvent thisFight = new FightEvent(this);
ALLOWSAVE = false;
thisFight.doFightEvent();
In that class, when the fight starts, I do:
ACTIVITY.setContentView(R.layout.activity_fight);
When I exit the fight, I do:
ACTIVITY.setContentView(R.layout.activity_main);
PLOT TWIST: This all works fine in older versions. I'm trying to make my game 100% compatible back to API 8.
Works great in API 8. API 19-22, not so much.
Help?

DialogFragment mysterious Null Pointer Exception

I cannot seem to make head or tail from the stack trace eclipse is giving me. The code used to work. I am not sure what I changed. But now when I launch the dialog fragment, the app crashes with the following error log.
FATAL EXCEPTION: main
Process: com.company.appname, PID: 8962
java.lang.NullPointerException
at android.support.v4.app.DialogFragment.onActivityCreated(DialogFragment.java:366)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1508)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:958)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5579)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
My code is massive so I am not sure which portion to show. So maybe someone has seen this mysterious kind of crash with apparently no root in my code (i.e. since eclipse is not naming one)
UPDATE
Ok. I find the culprit. But I still don’t know how to fix it. So I have CatDialog (the one that’s crashing now) and FacebookDialog. So I have had these two DialogFragments working independently. But now I am launching FacebookDialog from inside the onCreateView method of the CatDialog. That is the reason for the crash. The launching is conditional as the following code shows
protected void requestFaceBookPublishPermission() {
Log.i(TAG, "going into requestFaceBookPublishPermission");
Session session = Session.getActiveSession();
if (session == null || !session.isOpened() ||
!session.isPermissionGranted(FacebookDialog.PERMISSION)) {
FacebookDialog fb = new FacebookDialog();
fb.show(getChildFragmentManager(), "fb");
dismiss();
}
Log.i(TAG, "leaving requestFaceBookPublishPermission");
}
You are calling dismiss() from inside requestFaceBookPublishPermission and that's totally wack! You can't do that if you are calling it from inside onCreateView of another DialogFragment. So you seed to keep the two separated again. But great question though.
So understand what you are doing: you are dismissing before returning the view inside onCreateView. You are pre-empting.
I've encountered this issue before. Then I was dismissing the dialog inside oncreateView and I was getting NullpointerException. Then I moved my code to dismiss the dialog to onResume() and the issue was fixed!
Wrong
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
if(something){
} else{
dismiss(); //Dismissing dialog here gives NullpointerException
}
}
return view;
Correct
#Override
public void onResume() {
super.onResume();
if(something){
} else{
dismiss();
}
}

Xamarin Android Theme.AppCompat exception

I'm writing an app in Xamarin for Android, and all is going well, but all of a sudden, i'm getting this "java.lang.illegalStateExeption error when i run the program.
I have done nothing to the code that might have caused this, and i've even gone back to copies of the program that i've backed up, where i know there was nothing wrong, and it's still throwing the exception.
The exception is thrown on the first line of the OnCreate method, here:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState); // error thrown here
and this is the error:
You need to use a Theme.AppCompat theme (or descendant) with this activity.
At the top of the Main activity, i DO declare an AppCompat theme, like this:
[Activity(Label = "FrogPoint", Theme = "#style/Theme.AppCompat.Light", LaunchMode = LaunchMode.SingleTask, ScreenOrientation = ScreenOrientation.Portrait)] //master copy
public class MainActivity : ActionBarActivity, IBeaconConsumer
{
this is driving me nuts, particularly as it's just started happening for no apparent reason. Every time the app runs, the error is thrown.
I have tried both with, and without, the following in the Android Manifest:
<activity android:name="MainActivity" android:theme="#style/Theme.AppCompat" android:label="#string/app_name"></activity>
It makes no difference.
Can anybody please help?
Here is the stack trace:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:102)
at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:58)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
at frogpoint.droid.MainActivity.n_onCreate(Native Method)
at frogpoint.droid.MainActivity.onCreate(MainActivity.java:41)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
The problem was a corrupt Resource.Designer file. I deleted it from the project, then rebuilt the project and added the file back (it doesn't add it back to the project automatically). Then the problem was gone!

IllegalStateException: Fragment Already Added when resuming app

Overview:
I have a fragment-based app with a single activity. On launch, I add my main fragment and all works well. When I tap on something, I call replace() to swap out to the new fragment and add the transaction to the back stack.
Problem:
When the app dies in the background (or most easily repro'd with 'Don't Keep Activities'), and I relaunch the app from the home screen, I get the exception below.
Scenario:
1. Open app
2. Navigate to a secondary fragment (this problem doesn't occur if you never leave the main fragment)
3. Background the app
4. Let the app die naturally (~24hr), or enable 'Don't Keep Activites' in the Developer Options
5. Open app from home screen
Expected:
App opens to main fragment (regardless of what fragment was displayed when backgrounded)
Actual:
Crash below
Other Requirements:
- Background app and immediately tap from home screen must return to previous fragment (assuming it wasn't destroyed in the background).
Stack Trace (note absolutely no calls from my app itself, so nowhere for me to debug):
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rcg.socialstitch/com.rcg.socialstitch.ViewStitchActivity}: java.lang.IllegalStateException: Fragment already added: ViewStitchFragment{42800520 #1 id=0x7f040030}
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2227)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2276)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5146)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
at dalvik.system.NativeStart.main(NativeStart.java)
Caused by: java.lang.IllegalStateException: Fragment already added: ViewStitchFragment{42800520 #1 id=0x7f040030}
at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1133)
at android.app.BackStackRecord.run(BackStackRecord.java:618)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
at android.app.Activity.performStart(Activity.java:5320)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2190)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2276)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5146)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
at dalvik.system.NativeStart.main(NativeStart.java)
I believe I have solved this. My .replace() calls all added to the back stack, but my initial .add() did not. When I ensured my initial fragment addition was no the back stack, this no longer seems to happen.

Categories