In my App, which uses an AppCompatActivity, I have a main Activity and everything else is displayed using Fragments.
However, I have noticed, when I press the home button and do something else on the phone. When I get back to the app, it crashes with an "unfortunately application has closed" error.
What I understood from my research is that the Activity and everything else gets destroyed. Once the App is opened again, everything is lost and so the application don't know what to do and it crashes.
What can I do in a case like this?
I would be happy if when the application gets re-opened it will just restart, or even better just display resume where the application left earlier.
What can I do to prevent my app from crashing?
Check your Android monitor window or firebase console if integrated with firebase crash analytics for exact exception error message.
One possibility could be error something like this,
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1341)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1352)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
This can be solved by using commitAllowingStateLoss() instead of this commit().
refer this for more info http://www.androiddesignpatterns.com/2013/08/fragment-transaction-commit-state-loss.html
Related
As the title says, I'm looking for the cause of ActivityThread: Schedule relaunch activity: foo.bar.MyActivity in my application log. I'm seeing this entry after MyActivity.onResume() but I'm not overriding that method and I'm not restarting the activity myself. I found this question related to the log entry but I'm not issuing any network requests or doing anything complicated in onCreate(). There are no configuration changes in the log, either, that might explain the activity being restarted.
I can't reproduce this on my devices and I only know of one instance of it happening so it seems to be something that happens very sporadically. In short, does anyone know why Android will sometimes restart my activity with the message in the title?
This might be happening if the app is in the background for awhile, Android kills off the OS process hosting the app, and then the user returns to the app (selects it from the list of recent tasks, or just clicks on the app icon on the HOME screen again). Android will then create a new OS process for the app and relaunch the Activity that was on the top of the Activity stack.
What code is required to keep an app live when it is "pushed" to the background?
My app USED to behave this way. If a user multitasked or hit home, my app would minimise, and they could later return to it. Now, and I have no idea what change I made to cause this, my app is closed by the Android system whenever it is minimised.
I have checked memory, and it doesn't seem to be an issue. My app is actually using LESS memory now than previously.
When the app is minimised and closed, OnPause and OnStop are being called for the current activity, but onSaveInstanceState is not.
SOLVED: It was caused by android:noHistory="true" in my manafest
In Android when you hold down the home button you can bring up a task list of your recent apps. And each "card" in the task list should have a preview of the app. However, this time I opened the task list and the previews were blank (except for the app title).
I had the phone Settings in the task list and every time I would tap on the task and try to open it in would close immediately.
I also had this app I was developing and when I tapped on the task to open the app my RecyclerView wasn't showing anything, when it should be.
I also was playing AngryBirds 2, so was it that Android had silently killed those apps for memory? If so, does onCreate() of the killed apps get called again when this happens?
Edit: I just noticed if you close an app too quickly when it's transitioning to an Activity you can get a blank preview.
Edit 2: So to answer my question, after referencing the Activity lifecycle diagram this is what I found. When another application needs memory, the app's process will be killed and when the user navigates back to the app, onCreate() will be called again. However, that doesn't explain why I couldn't reopen the Settings. And I have no idea how to reproduce this weird behavior!
Ok after some debugging, I found out that Android did indeed kill my app because it needed memory. When you resume to your app however, onCreate will be called with a savedInstanceState that is NOT null. I had some logic that required savedInstanceState to be null and therefore my RecyclerView wasn't populated.
To recreate this scenario of Android killing my app I had to open a ton of apps lol. Is there way I can reproduce Android killing my app without opening all those apps? Hm...
The problem that I'm having is when running my android app, if I run into a force close error or pause the application with a breakpoint in debug mode and then press 'stop', the app doesn't quit, instead the first activity on my activity stack is opened but it is in a very buggy state. The activity is a library of books and when it is opened after the force close or by stopping, all of the users books are gone, the labels on the options menu are gone (although the icons are still there) and almost any action results in a force close.
So basically I'm wondering why stopping the application in debug mode, or running into a force close doesn't shut down the whole application and instead opens the first activity in a very buggy state.
I can't give specific code because the force close only happened once and I didn't get the stack trace. I realize this is a very generic question and I understand if it's too little information to go off of I just wanted to see if anyone else had run into something similar.
Edit: It seems as though force close just closes the current activity and tries to open the previous activity on the stack. However somehow my application context is getting destroyed so when the previous activity opens and looks for information in the application context, such as books in the user's library, there is nothing there.
When you run into a force-close, your app's process is killed. If your activity stack had an activity behind the one that crashed, it is restarted in a new process. When the process dies, so does the application context. The new process creates a new application context, which is why you're not seeing your data in there.
See http://groups.google.com/group/android-developers/browse_thread/thread/b274cfa64b17f535?pli=1 for a discussion on this.
One way to address your specific problem is to add a flag in your Application object, that you explicitly set to true after your shared data is loaded. You can then check for this flag in your Activity's onCreate() to confirm that the data is available. If the flag is false, you can call finish()
This has happened to me a few times. I've noticed that when I have a stack of activities and I start the second without closing the first, then a "force close" on the second only stops the second activity. Since the first activity is still running, it comes to the forefront.
Example. Activity A is running. Clicking on a button opens activity B. (I didn't finish() activity A) Activity B for some reason 'force closes'. Activity A comes into view since its still running.
i am working on an application.The first screen having login.After launching the application in device and playing with it is all ok and not any issue at all.
but if at the same time user opens any other application,uses it for a while and closes this second application and then again navigate to previously open application(on which i am working),and tries to tap or manipulate or login on the application ,application gets crashed. why so behaviour?
i am facing this issue very first time and no any idea of this.
any suggestion?
Thanks.
Check out the Android app lifecycle: http://developer.android.com/reference/android/app/Activity.html.
My guess is the Activity that your user is coming back in to is overriding the onResume() method and the method is throwing an exception or you're not calling super.onResume(). That's my best guess without any more details on what your code is doing or information from your logCat.