Using my mom's phone with my app if i hit the home button and then navigate back to the app, it is calling onCreate again. If I understand correctly this should not be the case right?
I know this is happening because my webview loads and it should just open up the page that was already open. Especially since I have the launch mode set to singleInstance...
What are some reasons to why it would be called twice on my mom's phone, but on my phone it works exactly how it should?
According to the Android documentation, your Activity can be killed at any time when it's not active which is the case when you switch back to the home screen.
If an activity is paused or stopped, the system can drop the activity
from memory by either asking it to finish, or simply killing its
process. When it is displayed again to the user, it must be completely
restarted and restored to its previous state.
Related
I have an issue where I want to differentiate between an activity being stopped because the user wants to navigate back (staying active within my app), or putting my app into the background (switching to the home screen or another app).
Is there any way to tell during onPause or onStop which of the two is the case?
I have made an app.
When I launch that app in the normal way, then it works fine. When I press the home button, the app is paused and is still there as it should be.
Now I want to start my app by an intent...so e.g. when a NFC tag is detected the app should launch. This also works, but when I am pressing the home button then the app disappears, but I would like that it stays open (in pause mode) like when I launch it in the "normal wayy". Does anyone know why this can happen? My observation is that the onStop() function is called but not the onDestroy() function. Therefore it is very strange that the app just "disappears".
Thanks a lot in advance.
Please see Android Activity Lifecycle. onDestroy() is not always called when application goes to background and generally here are no guarantee that onDestory() will be called before app/activity is killed.
Could you please explain what you mean 'pause mode'? You mean that app saves the state?
I am starting a service in my app. On click of service I am launching an activity. The service click event works fine when we go to any app and press back button and exit the app.
But if we are in any app and then press home button and click on the service the activity is not launched. If I click it more then 2 times,it opens the activity and sometimes it opens the app also.
So i am unable to understand the difference between pressing back button and home button.
After you start an activity, if HOME key is pressed, then the current activity is stopped and its task goes into the background. The system retains the state of the activity - i.e. onSaveInstanceState will be called. If the user later resumes the task by selecting the launcher icon that began the task again, the task comes to the foreground and resumes the activity at the top of the stack.
However, if BACK key is pressed, the current activity is popped from the stack and destroyed. The assmuption is the activity is done and will not be used again. So the system does not retain the activity's state - i.e. onSaveInstanceState will not be called.
Home Task :
Pressing the Home switches you from the app to the home screen, whilst leaving your app running in the background. This is a bit like switching between windows on a Windows PC.
Except that when your phone is running low on resources like memory it will start to close apps that are running in the background, so that your phone has enough resources for what you're trying to do now. Games are often amongst the first apps the phone will "kill" to save resources as they often use a lot more memory and CPU than other apps. This is why sometimes your game is still running paused, and sometimes Android has closed it for you.
The Back button is the way to close apps so that they are actually closed.
onPause() is called in Activity A when it launches Activity B. After the back button is called in Activity B, onResume() is called in Activity A.
In case of activities their default implementation is LIFO based in stack and works like:
On Back button Pressed: finish the current activity by calling stop method.
On Home button pressed: activity is being paused and then it may either resume if come back to it, otherwise system will call stop() method of activity to save unused resources and utilize memory.
but these functions can be edited by overriding if required.
I have a puzzling problem. If I deploy my application from Eclipse on any device/emulator, I can use the Back and Home buttons to minimize it and then I can click the launcher icon to resume it.
If I take the apk, put it on the phone and install it (using the Astro file manager), then press Back or Home, when I click the launcher icon again, my application will be recreated instead of resumed: the activity is not destroyed.
This goes against everything written about on the android developer website regarding activities and their lifecycle.
For instance, this is what I do when the user uses the Back button:
#Override
public void onBackPressed() {
moveTaskToBack(true);
}
Yet my application is recreated when I click its launcher icon. Any ideas?
This goes exactly with everything written about the activity life cycle.
If an activity is paused or stopped, the system can drop the activity
from memory by either asking it to finish, or simply killing its
process. When it is displayed again to the user, it must be completely
restarted and restored to its previous state.
http://developer.android.com/reference/android/app/Activity.html
You application is not "minimised". There is not such thing as minimised in Android. It's not Linux or Windows or OSX.
It's true, pressing Back will kill your Activity. However, why is that a problem? Android provides a mechanism to save your Activity's state and be able to resume.
View the Activity lifecycle: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
This is a must read on saving Activity state: http://developer.android.com/reference/android/app/Activity.html#SavingPersistentState
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.