Android Intent Activity crash behaviour - app restarts halfway through the app - android

I have a pretty standard iPhone app that creates a series of around 7 unique Activities initialised by Intents.
However if the app crashes on the 7th Activity, the app restarts on the users phone around the 5th activity. The problem then is the info gathered from activities 1-4 is null, meaning the app is useless and the only way to get the app working again is to either continually press back or else kill the process.
Why does this behaviour occur, and is there a way to force the app to start back at the first activity when it crashes.

Your app is restarting in the activity that was beyond the crashed activity on the activity stack. You can finish all activities that are beyond your current one through calling
this.finish();
after starting the next activity.
The problem is that the user now can not press the back button to change data that was inserted in the steps before because those activities are gone.
You may have a general problem with data persistence over pause and resume cycles. Try to call your emulator or phone while you are in one of the deeper activities and then return to the app through long pressing the home button. You may see that the data from previous activities is now also empty.
Play around with this behaviour and have a look at the application life cycle documents.
This may be a way to check if data is available and if not close the activity or go back to the start activity.

Related

Android Activity is recreated when activity from background(pause) to foreground

Hi Android professionals,
I fell into a small issue about some activity life cycle. I mentioned my problem with step by step:-
launch the app with app icon from home screen.
Enter some data into a edit text fields in recyclerview in activity A.
press the home button and launch the other app(any app like facebook ,twitter).
now our app is in paused state and in background with empty values.
now click the recent apps button and launch our application (from background).
now the app is launched with Activity A but that activity is recreated .
I was also disable Don't keep activities in Developer options.My Testing tab is Levono A8 50.
I wasted 3 days time on this issue. Any one have any ideas on this issue.
thanks
Not a problem but feature - Your activity can be killed and recreated after losing focus as Android OS wishes - get used to it. Your activity shall save all necessary state in onPause()
As soon as your current activity is not visible anymore after launching another app, it is in a stopped state. You can't however count on the activity remaining in that state. Android will destroy the activity if in need of more resources (and then recreate it later on). If you want data to persist, you need to store them using SharedPreferences or a database in onSaveInstanceState().
Check here for more info on the matter.

What's the difference between opening an app from the applications screen and the recently used apps list? (android

Can anyone tell me what the difference is between opening an app from the applications screen and opening it from that recently used apps list that pops up when you long-press the home button?
I didn't even know that recently used list existed until a friend managed to break my app by launching it from there. He tried twice and got the same force quit, but when he launched it from the applications screen it opened fine.
The error log told me that a nullPointerException occurred in the getCount method on my ArrayAdaptor for my ListView.
Anyway I just wondered if there was a difference that I need to know about and adapt my code to deal with?
AFAIK, If your application is completely shutted down, launch from applications screen and recently used apps list should have no difference, both refresh start your application and open your application's MainActivity (by stack-push your application's MainActivity into a newly created task)
However, as Android is multi-task OS, your application can be put into background in standby mode i.e. open your application then short-press home button, this is not same as press back button. If you haven't override these key pressed in your application, press back button several times with pop all your activities off from activity stack and finally kill your application, whereas press home button will bring System's HomeActivity into foreground hence flip your application (AKA. task with activity stack) into background.
Things becomes more interesting here, depend on which value your configure your activity's android:launchMode in AndroidManifest.xml, if you use standard or singleTop:
1. launch app from recently used apps list always bring your standby activity back to foreground, i.e. re-order activity stack.
2. launch app from applications screen will create a new instance of your MainActivity and open it, i.e. push a newly created MainActivity into activity stack, so now you have two instances in your application's activity stack
If you use singleTask or singleInstance:
2. launch app from applications screen will use the standby MainActivity (if exist) in your application's activity stack and re-open it, i.e. re-order activity stack.
Checkout Tasks and Back Stack to see how different configurations may affect your application's activity stack behaviour.
I believe there should be no difference. These are the lifecycle methods I typically see when pressing the home button from an activity, on android 2.3.4
onPause
onStop
then when I use either the icon or previous applications to navigate back, I see
onRestart
onStart
onResume
Now, in some cases the system will tell your activity to finish while you are away (or immediately when you return if an orientation change occurred). Then you will see onDestroy, and the following when you navigate back
onCreate
onStart
onResume
I don't think there is anything mysterious going on here. According to the Activity documentation, there are only four states that a process can be in, and both of these fall under background activity.
There shouldn't be any difference in how the activity is launched from history, apart from the fact that the launching Intent will have the FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY set.
Here's an easy way to think about it. All of your activities are launched form Intents. Holding down the home button allows you to open that activity using the last intent that launched it. This can give you some unexpected results however. For instance, if you are able to launch your activity from something special like a widget.

Is there any chance to remember Activity stack so when I start app next time I have same Activity stack?

Is there any chance to remember Activity stack so when I start app next time I have same Activity stack ? For example when I start app I am on Activty_1, then I go on Activity_2 and then on Activity_3 when I stop app ( home button, app is in background until system removes app). When I start app next time I need to be on Activity_3 and when click back_button ( there is back_button on every layout on my activities and call just finish(); ) to go on Activity_2.
If your app has been completely shutted down and fresh started next time (from either app launcher or recently used list), AFAIK, there is no easy solution to retain Activity stack (it is actually called back stack from Android official dev giude) as it always get fresh created every time you fresh start your app.
If your app has been sent in background (by home button) and started next time (from either app launcher or recently used list) which is actually bring your standby app to foreground. The default configuration android:launchMode="standard" should gives the exact behaviour you described.
The behaviour of back stack navigation is quite flexible and adjustable via numbers of configuration settings, A worth reading from official dev guide talk ablout Tasks and Back Stack.

Force close doesn't exit android application, instead opens a previous activity in very buggy state

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.

Return to last Activity

In most Android apps, when you press the Home button to "minimize" the application and then open the application again, you will be taken to the screen you were last on in that Application.
I've read the following:
When launched via icon on the home
screen, Android will always start the
activity with the
android.intent.action.MAIN filter in
your AndroidManifest.xml, unless the
application is already running (in
which case it will obviously restore
the activity on top of the stack).
but this is not happening. When I launch the application a second time, it takes me to the main Activity. The application isn't being terminated. If I then navigate to the screen I was last on, all the data and such is there.
How do I make it do what I want? The related questions all seem to pose the same answer (the one I quoted above), but this is not holding true.
Are your activities in the same task? That is, are you starting any of your activities as FLAG_ACTIVITY_NEW_TASK? I'm not positive, but that could change on the resume behavior works.

Categories

Resources