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

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.

Related

How to check if my app is in visible background if it calls another application activity on android

My application launches activity of another application (sms, e-mail or another...). How do I know if my application is still in focus? Whether the user has minimized it? Was it overlapped by another application (not called from my application)?
Detailed Description : Here
The ACTIVTY Lifecycle describes all the states your activity can be right from when your app is started till when it is destroyed(maybe by the user after clearing from recents menu in modern phones or is killed by the system due to lack of resources)
This short image with descriptions will help you understand which method is called exactly when and is also open to you experimenting.
So in your case as to when to detect when another activity is in focus, it is when the onPause method of your activity is called but I recommend going through all the other methods

How to stop calling onCreate again when launching application in kindle fire device?

I developed an android application with two screens. The application is working with android devices good. But i installed application in amazon kindle fire tablet launched the application started good. I navigated from 1st screen to 2nd screen and pressed home button.
And again i launched application from applications menu. application is showing 1st screen instead of 2nd screen. Common behavior of android is should show the second screen.
What i know is while press on home button the application will go for onPause() state and launch application form launcher icon it will go fore onResume and show the screen where it has previously.
But application every time calling onCreate when launching from launcher icon. This happened in Amazon kinlde fire tablet only.
I am starting the 2nd activity from 1st activity using startActivity(intent); method
May i need to use any flag for kindle fire tablet.
Please suggest me as ASAP.
Thanks in advance.
Android doesn't guarantee that once you press back from an activity, it will remain in the onPause state. It depends on how aggressive the memory killer is, or how low the device memory is. What this means is, don't count on your application being in onPause state ever.
What you could do is, save the activity state in a shared preference, and let the Application object launch the corresponding activity, restoring UI state if required.

Android: How do I restart my app by activity that I stopped by pressing the home?

When i stop my application pressing home button, i need that the app restart from the same activity when i open it again.
Now my application always start from main activity :(
(i don't know why but on emulator work correctly...)
There are a lot of options for preserving the state of an application - the ost common of these include using the bundle passed in OnCreate to initiate the application correctly.
I suggest you look at the android developers documentation as regards to activity lifespan.
When you begin an Activity, store a SharedPreference that records which Activity you are on. Check this preference when you enter your main Activity and, if set, jump to that Activity.
(If you are just looking for a way to get back to your Activity and don't care about the code, use the Recents menu to get back to your Activity. On devices with dedicated keys, long-press Home.)

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.

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

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.

Categories

Resources