I have a serious problem with my App continously crashing on some users devices with the following exception in an Activity onStart method:
Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1299)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1310)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:541)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:525)
at android.support.v4.app.DialogFragment.show(DialogFragment.java:123)
I cannot reproduce this error locally in my emulator. I have found a few questions regarding this issue on stackoverflow:
here and
here
I have tried the suggested solution, i.e. do not allow empty bundles in onSaveInstance neither in fragments nor activities.
However the problem remains. This is the only Crash report I ever get, and I get it continously.
Does anyone have a working solution?
I have solved this issue by
upgrading my support lib to revision 10
using workaround posted here: http://code.google.com/p/android/issues/detail?id=23096
i.e.. Add a non-ui fragment in onResumeFragments and invoke further transactions in Listener Callback.
Check my answer here for code: "Can not perform this action after onSaveInstanceState" - why am I getting this exception from my activity's onResume method?
Got no more IllegalStateException so far.
I solved this using this workaround, namely put the code into a methods onPostResume() function in the Activity
Read This Article.
And,
This one.
Once the issue is understood, go ahead and change things properly:
Figure out why your transactions are happening outside your onResume state. Why are these things happening when they shouldn't be. Why are you manually adding and removing fragments with regard to the lifecycle of you app rather than when the user requests. You should maybe need one time in the onCreate() or when the user invokes something. But why are you messing with them in onResume() in the first place? Don't do that.
If you change all the .commit() to .commitAllowStateLoss() then the crashes will go away. But, you're better off not doing that. You are better off making sure you never change your fragments except when the app is fully live. Sometimes that means never loading a fragment from the OnActivityResult() but rather flagging to run in your onPostResume(), and avoiding performing UI changes in async threads that wrongly assume you couldn't have killed the activity in the meantime.
If you just want to spackle everything, .commitAllowStateLoss() will do that. If you want to do it right, make sure you don't fiddle with your fragments after things die.
One of the more recent forms of Android circa Oreo I think, changed this to avoid this annoying error. My answer seems a bit vague but it's because the bug is in the paradigm. You entirely can hit that with various code elements and fairly routinely.
Related
I know this might be a basic question, but I am a beginner and I haven't been able to find an answer.
I would like to know, if it is possible to trigger some function/action while creating an activity. eg. trigger a void without user intervention, start an animation automatically after an Activity is created etc.(at the moment I am trying to trigger an animation without a button click, but this question is general and not specific code related).
I know how to bind an action to a button click and every resource I have found is doing that, I haven't, however, been able to start an animation or a void without it and I am not sure if it is possible. (I have tried using Handler, it worked but prevented the rest of the code being executed - I might have done it wrong, though as I really am a beginner)
so my question(s): Is it possible to trigger a function/animation without user intervention(click)?
If so, where can I find some resources to read about it? (I tried reading Android documentation already)
Thank you very much and I apologize if the question is too simple, I know it might be.
Please have a look at this photo.
It clearly explains the lifecycle of Android activity.
To answer your question, yes, it is possible to trigger a function/animation without user intervention(click). You have to call the function from onStart() activity.
I am experiencing a rather subtle and undetectable bug in one of my Android app's activities. Under certain conditions, when I transition the activity from landscape to portrait, or vice-versa, the app crashes. What is particularly difficult and annoying about this bug is that none of the breakpoints in the activity are being hit in debug mode when I do the transition. In addition, logcat does not seem to be much help, and nothing shows up there either.
So I would be looking for some suggestions about what could cause a crash on orientation change. I can post some code, but I obviously can't post everything, and I'm not sure how much help it would be to post a few fragments from the activity.
The lifecycle of an Activity is such that on orientation change events like onCreate() and onStop() (for example) are not called. So, if you have created objects in onCreate() they might not be initialized (=null) when your Activity returns after being rotated. Check your code for objects that might be initialized in onCreate().
Also you might want to add a few more try..catch with Log.e messages.
To be honest, if your app crashes, there should be an error logged in the logcat.
I was on the verge of giving up, when something turned up in Logcat after one of the failed tests:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
com.google.android.gms.maps.model.Marker.setIcon
(com.google.android.gms.maps.model.BitmapDescriptor)' on a null object reference
I followed the stack trace to the point in the activity where the null pointer exception happened. In fact, as many of you suggested in comments, there was some state which was not being restored properly. The exact problem was happening in the initialization of a Google Map.
At first glance now, this question seems to be of the "why isn't this code working" type. However, it is not, because both debugging and, most of the time, Logcat, failed to reveal what the problem was.
As a take home message, if you ever encounter bugs in your Android code, you should not rely on debugging alone, because it might not work. Logcat seems to have come through here, but even it seemed to not be reproducible every time.
I am making an android app on API 15 for a mobile computer application.
In this app I have a background service, a lot of classes inheriting from AsyncTask, and some sql updates and broadcasting to the service on the app pausing.
I have recently discovered an issue where going to a specific screen in my app, hitting the home button, bringing up my app list, and swiping away my app to kill it results in the app closing and then soon restarting itself at the main activity. It should just close normally.
I have tried placing breakpoints everywhere, and even disabling the background service, but i cannot seem to find any indication of what is restarting the app when it is closed.
I just tried getReferrer inside a try catch and my app says it isnt responding and shuts down even though its in a try catch block.
Both problem seem odd to me.
If it means anything, Android studio will still be debugging the app once it has opened again after i close it.
If anyone could shed some light on what could be restarting my app after i close it, it would be very helpful.
EDIT:
I am using Start_Not_Sticky. this and Start_Redeliver_Intent are ideal option from what i have read.
Thank you to Kevin Krumwiede for suggesting grepping all my code for the main activity's name. I had no idea I could go through all of the code in my project within the IDE instead of going one file at a time.
I had previously tried going through all my classes and searching each individually for reference to it with ctrl+f but i never considered looking in one of my activities that i hadn't modified in months for the new issue.
when I used ctrl+shift+f it all came together as i saw the startActivity function inside of OnDestroy(), not sure how i never noticed this issue before.
Ever since I started using one of the newer devices from Google for testing, my tests have gotten stuck whenever we call waitForIdleSync(). When going over the documentation for espresso for another reason, I found this amazing tidbit: "To avoid flakiness, we highly recommend that you turn off system animations". See the main page and how to do it programmatically.
However, I've followed the instructions (disable animations and restart my device) and my tests are still getting stuck. Google's documentation has a habit of getting stale fast, so, is there something else I need to do to get this working on newer devices?
I have the same issue and it broke our tests completely for the last two days.
Problem
I researched this issue a lot. In fact it is an AOSP bug: https://code.google.com/p/android/issues/detail?id=82046
(re-opened from this https://code.google.com/p/android/issues/detail?id=61111)
The problem occurs when you have a a animation, loading spinner, progressbar etc. in your Activity/ Layout that runs for a long time.
waitForIdleSync() will therefore wait forever.
In our case it is very problematic, because the methods getActivity(),launchActivity() and launchActivityWithIntent() of ActivityInstrumentationTestCase2and also the new AndroidTestCase have waitForIdleSync() inside.
Solution Suggestions
You can try turning the animations off or set the Visibility of any ProgressSpinner to GONE via an Intent Flag and start it with that. (Personally, I don't like that solution as it would affect the Activity itself and not the Testcode)
Another way would be to mock the preconditions for the Activity to let it not fall into this state.
Other suggestions are welcome!
Under some circumstances I need to be able to tear down all my activities that are in my application stack and recreate them all due to configuration changes. I have accomplished this by first calling finish for each activity and then recreating the stack.
To recreate, I relauch my root activity. And within its onStart I have it create my second activity. Within my second activities onStart I have it create my third Activity. This does work but the problem that I am having is that when watching the screen you see each of the three activities created and animate into the next activity. I want to have this rebuilding invisible to the user and hide these transitions. Does anyone know how to accomplish this?
Android already takes care of restarting activities when there is a configuration change.
If you are saying you want all of your activities to be restarted, even if they aren't currently visible (Android will do this lazily as the user returns them and they become visible, if the configuration is still different at that point), then no there is no simple way to do this. I can't imagine you coming up with anything that isn't going to be hideously ugly, because to get the platform to restart your activity you will need to make it visible, and then you are going to have flicker up the wazoo.
Things just aren't intended to work that way. This isn't how pretty much any other application you run on Android will operate, so if you deeply feel like it is something you need to do then it will be useful to explain why that is so we can tell you a better way to accomplish what you want. :) For example, if you have a bunch of activities whose state is fundamentally tied together to require this, consider using fragments instead (or cleaning that up).
On the other hand, if you just have some internal concept of a configuration and want to get your activity to be restarted (say for example to switch between themes), there is an API for this but it only was added in Android 3.0: http://developer.android.com/reference/android/app/Activity.html#recreate()