I have two activities: Login and List.
When i log-in i display the list. When user is in the list activity and press the home button. After that clicks on the app icon it brings to login page for some times only. While it should display the list.
How to resolve this?
Your app is pushed out of memory sometimes in order to free memory for other apps. When it happens, app is re-launched when user enters it and first Activity is opened. The only(or not the only) way for you to maintain your app state is to store it somewhere. You could save whether user logged in or not in SharedPreferences and when Login Activity is created you could check this.
Sometimes your app will be killed depending on how much memory other applications need. Your application must save and restore its state in order to behave as if it hadn't been killed.
If you read the section on the Activity Lifecycle in the developer docs, you should understand what you need to do.
Related
Can you please explain to me why my activity which is currently (onStop) is go to OnDestroy event and restarting again after clicking on the app icon in the interface.
Because of that i have problem with user data saving. I'm using event OnDestroy to ask user if he want to save the data. Because of my misunderstanding when i collapse the app and click to it again i am waiting to my activity to have all the data in it but instead i see saving dialog from the previous activity whiсh goes to onDestroy event.
Other apps don't act like this.
What should i check?
Your description is not very clear, so my answer may not help you.
Research the app life cycle here: https://developer.android.com/guide/components/activities/activity-lifecycle.html
Your app will enter onStop is before onDestroy. This can happen if user activates another app. (So the user may no longer see any prompts that you display e.g. To ask to save or not.) Android may choose to destroy your app based on its rules after that point, even if the user clicks on the app icon to start it again.
Perhaps you can test what happens if the user switches back to the app (not clicking the icon), but use the "Recent apps" button and select it.
I have been looking into storing data for my app and trying to choose between different methods (onSaveInstanceState, onPause/onResume) and different methods of storing(states in onSaveInstanceState, SQLite, Prefences).
I am curious what happens to each of these methods of storing when the user does certain things. In specific, I want to know what methods are called and what data is wiped when:
User clicks the task switcher button(bottom right) and then closes the overlay within seconds, all from within the app
User clicks home without swiping out app from task switcher, then reopens app
User clicks home, swipes out the app from task switcher, then reopens app
User exits app, restarts phone, then opens app
User exits app, and uses Clean Master(or any other storage manager) to clear the cache of all apps or kill all background tasks, then reopens app.
User updates app
I'm trying to store highscores. Currently i'm just using
savedpreferences and putting 5-6 numbers in an editor, committing, and
continuing. there might be a more efficient method, which is why I
elaborated on different possibilities above.
I assume you meant SharedPreferences. And yes, you're doing it the right way. Your high scores will be saved between app executions. And doing the same with SQLite would be way overkill.
The only way that data can be cleared by the user will be if he clicks on Clear the Data or if he clicks to Uninstall your application
However, neither uppdating the app itself, nor clicking on Clear the cache will do anything to the SharedPreferences, the user will have to press on that button "Clear the Data" if he wants that specific data to be cleared.
As to a more efficient method, it depends what you mean by that. What you're doing is what almost every developer would be doing. It's the simplest solution for the job that actually works. For instance, if you had chosen to store that data in a bundle between activities, then all that high score data will be lost between app executions.
That being said, your solution would obviously not be enough if you were to play the same game on multiple devices, or if you would migrate to a new device, or if you were to uninstall the game and reinstall it at a later time. For that, you may want to consider using Google Play Services to store that data for you, both locally and on the cloud.
When the user goes to task switcher and closes the app, then the app process will get killed and any services running in the background will also be stopped
When the user clicks home, without swiping out from the task switcher and reopens the app, then the app is getting resumed. You can get further details if you search for activity lifecycle
When the user clicks home and swipes out the app from task switcher, then it is equivalent to killing the app and the process will get killed and any services running in the background will also be stopped
When the user exits the app and restarts.reboots the phone, then the process is killed and services also will get killed. But after restarting/rebooting the device, user would have written logic to restart it once the device is restarted or app is killed
When the user exits the app and clears cache, then I would assume that the data is cleared. So this is like a fresh app.
Based on my understanding, I have given my brief answer. Hope this gave some insights.
I want to make application with activity "UserIntro" that shows ONLY when app is installed and asks user some questions about himself. But latter when user starts app again that activity doesn't appear.
Does anyone know how to accomplish that?
Thank you for the answers!
Use Shared preferences to store a boolean first time the app launches. As the boolean won't exist first time, just check for it's existance upon startup and manually launch an activity.
I'm working on large project, so there is some logic for saving application state, and then opening correct activity(fragment) when it comes from background.
But anyway, I've found that if user navigates through my app and then minimize it - android opens it from background in different ways in following cases:
User taps on app icon (behavior: user see home activity, doesn't matter where he was, when application was minimized)
User select app from android "recent apps" (behavior: user see exactly what he saw, when the application was minimized)
So, what is the difference between launching application from background by this two ways? I always thought, that it is the same mechanism, but, obviously, I was wrong.
Thanks for any answers
You should pay atention on the folowing docs Activity and Tasks. In short words: if user start app from recents you will receive onRestart before onStart (without onCreate it means that your app was just "suspended"). You able to save screen state using onSaveInstanceState(). But in general starting from icon and from recents - different application behaviors and you should provide proper code for this ways.
UPD
As described below root cause of unexpected behaviour was additional lunchmode attribute.
From what I experience as an Android user, both are same.
The difference we usually see is how we close the app
Press back button until app close / finish()
On this state no matter how we open the apps it will go to the main screen
Press Home button
On this state depend on the app. If the app does not handle any Activity the app will same with the first state. But if the app handle something like when onPause() the Activity then finish() the apps, then whatever you open with app icon or recent apps will provide the same result.
Correct me if I am wrong
I have an stock market application. When user login to application, server sends me a session key. This session is valid until user logout the application or don't do anything for several minutes.
My first problem (that is solved already) was when after login to application, user click home button and after that click on icon of application. Now, after login, a message (sent by server) showed "your user id is in use". I have solved this issue by saving session id in shared preferences.
Now, the client reported that we have same issue based of this story.
User login to application
After that user clicks home button
User goes to settings/applications/manage applications
Selects application and then in "Application Info" page, clicks on Force Stop button
User comes back to application list again and selects the application
After login, "Session Expired" message will show and application directed to login page again
Now, when user try to login, the above message ("your user id is in use") will show while session id is not expired.
I want to know, what "Force Close" button really do? Does it clean all instances of activities? When I click and hold Home button, I can see that I still have an instance of the application in memory. Therefore, what is the role of "Force Close" button?
Now, if user enter his/her password "your user id is in use" will show again.
When any application is ForceClosed.
The Application is Closed and Android Clears all the memory occupied by the Application.
That's why when you force close the application your static data member's are destroyed and removed from the memory.
Here,You can use SQLite DataBase for better performance.
ForceClose Means : Close the Running Application and Free the Memory.
Your question has been answered, but I'd like to add one point. You mention that
when i click and hold Home button i can see that there is still i have
instance of application in memory
Please note that what you see when you long press the Home button is the list of recent apps and not the list of apps currently in memory. Hence, even if an app has been force-closed, you will still see it in this list since it was recently used.
Actully when you just back & exit to the application....
see at phone setting, application.
your application was not close at our phone.
that is still alive & it runs at backend.
after force close application it close from backend also.