I'm making this android app that acts as a calculator for a game. The calculator will tell you the cost of everything you selected and also show you a total price.
For example, if you select AR on the spinner, it will say it costs 1200. If you select WS, it shows the cost as 2400.
However, if this app is minimized for a great deal of team (30 minutes or more), the price values get reset to default (0). The spinner is still be on AR, but the price says 0. I need to click on the spinner again before it recalculates the value.
Is there a way for me to refresh it or prevent the loss of values?
It is due to that View is refreshed because when you hit the home button it will call the onPause() method then if you open another app it will then grant some memory in the others app and instead of going back to the onResume(), it will go to onCreate() due to the memory management of android.
solution:
you need to save all your data in the saveInstanceState of protected void onSaveInstanceState(Bundle outState) {
method then in Oncreate get all the saved data
You need to save your data in saveInstanceState method and then restore it in onRestoreInstanceState. After that you need to fill fields as it was in view.
Example
This is due to the nature of mobile devices having relatively limited resources.
You should save your data somewhere more durable. You might find this article on general Data Storage to be useful. This question should be relevant too: How do I save an Android application's state?
onPause in Activity - save your values in SharedPreferences or in Application (not Activity).
onResume in Activity - update select items of your spinners from this values.
You can save your data to Shared Preferences and in onResume() method you can set that value back to the respective field.
Changed text at the runtime in textview but after relaunching application retrieving default data in textview in android
Related
Ok so, there's a list of editText that the user fill with all names of players, say it's activity 2. And then click on start and play the game on activity 3. When he comes back to activity 2, the names are still in the editTexts and he can just add a participant if he wants and that's okay, but when he backs off in the menu, activity 1, and then open the activity 2 again, all the names are erased.
Is there a way to save the editText content in their places even when he leaves the activity 2 and comes back in it after? How can you achieve that?
Can you avoid calling the destroy method when back is pressed or do you need to save them somehow and put them back in?
Thanks!
If you want the names only during the application is alive, you can store the names in a Singleton class.
Or if you want the names even if the application is killed and opened again, you have to store it in a persistent storage. Either SharedPreferences or a Database (SQLite).
Check out the docs:
https://developer.android.com/training/basics/data-storage/shared-preferences.html
https://developer.android.com/training/basics/data-storage/databases.html
According to Save data in activity's onDestroy method You should save your data in the activity's (activity 2 in your case) onStop method. Technically speaking, you could then save that wherever you wanted and pass it back to activity 2 in the onStartActivity intent.
I have an android requirement where i have created a form like structure have radio buttons, edit texts and so on. When the user make half entries to the form and say moves away from the page either by back button or battery off. When he returns to the form, the same state of half filled form should re appear. Please suggest if android has internal functionality to save the form state and restore when the user revisits. Also, the stored contents should be saved only for particular duration say one hour after which the fresh form should appear. Please help with possible methods applicable for me to start with.
I recommend reading the Android Activity lifecycle. From the webpage:
"In addition, the method onSaveInstanceState(Bundle) is called before placing the activity in such a background state, allowing you to save away any dynamic instance state in your activity into the given Bundle, to be later received in onCreate(Bundle) if the activity needs to be re-created. See the Process Lifecycle section for more information on how the lifecycle of a process is tied to the activities it is hosting. Note that it is important to save persistent data in onPause() instead of onSaveInstanceState(Bundle) because the latter is not part of the lifecycle callbacks, so will not be called in every situation as described in its documentation."
In my application I have a filter function on table view which give filtered result in table. I set filtered options(checkboxes) in shared preferences so the next time the user go in filter screen it sees the checkboxes checked so it know what was the table filtered for and what he did check last time for filtering.
when the application launches i set them all to unchecked(requirement), but i dont know when the application is launched or resume. when the application is resumed from memory it again initialized all my data types. how can i check the application is resumed. if i set things in activity onResume - no gain it is called every time i just want to know only when the application is resumed.
What happens now i dont know when the application is resumed from memory, my filter behaves like application launched and set all to unchecked
what i did is, on my first activity made a boolean and put false and stored it in shared preferences, then did it true when start that filtering thing. so it remains true as the application remains in the memory and resume even. when application exits, and the launched again on startup it again turns false
There's no "resumed from memory" state. It is either resumed or created (or restarted). So you should rework your application logic and init your stuff on onCreate() instead of onResume()
I am not able to understand you question properly, but as far as my understanding goes, you are filtering user options through shared preferences. If this is so, you need not unset/set each option programatically. Android remembers and and restores this for you. If you want to access these, just call context.getSharedPreferences() and from sharedPreferences object received call getBoolean (key,defValue).
You can restore your checkboxes on onRestart(), that means your activity was not killed and is resumed after it had been stopped(either manually or by android).
I have a text field which indicates date selected by the user.Also i have provided a button for the user to change the currect date.
The problem i am facing is as follows.
1 . In the Oncreate method i have set the textfield to the current date.When the activity gets created it displays the current date.
I use the button and change the date to someother date.
All this works fine..
But when i rotate the device the oncreate again gets called and textfield gets changed to the current one. I want the textfield to retain the user selected one.
Please help me in this issue. Is there any way to prevent this ?
Screen rotation kills your activity and causes the entire lifecycle to restart. When this happens, onCreate will still have access to the original intent which began the activity, and certain widgets may retain some of their state (EditTexts, for example), but much of your dynamic Activity data will be lost. To prevent this, look into using the onSaveInstanceState method. This will allow you to put any important data members into a Bundle before rotation kills the screen. This Bundle is then passed to your onCreate as the parameter savedInstanceState. Here are the dev documents on this:
http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState%28android.os.Bundle%29
In your case, you'll want to save your date value by putting it onto the Bundle as an extra. Then, in onCreate, check if savedInstanceState is not null, indicating that it contains your date and handled it accordingly. Also, make sure that if you set a default value for this text later on in the Activity Lifecycle, it does not clobber your retrieved date.
You need to save the variable before rotation. I think that the best option is use the Application class as a place to save variable and do it in onPause() function.
I have 7 activites all with back and forth navigation buttons between the rest; activites consist of editTexts, Spinners, textViews, TimePickers, DatePickers, and checkboxes.
I want all UI to be present and saved through navigation of an application instance; however on application termination everything needs to default.
My 8th activity collects all UI and places into an email . . .fyi
I have read alot about both onSavedInstanceState & SharedPreferences way of saving the data as activities go back and forth . . .
Which would be better for me?
It will depend on how you want to manage the data. Both options (and more) are feasible:
If you want to fill once and keep the data even if the app gets killed, use SharedPreferences.
If it's volatile data that will have to be reentered differently some other time (i.e., days later), then use onSavedInstanceState.
If you want to keep multiple datasets on the same device, then use a SQLiteDatabase
SharedPreferences
Use for things that should always be remembered, no matter if the phone is turned off (eg for settings chosen in the settings screen of your app
onSavedInstanceState
Use this for remembering things about the current state of your activity such as the currently selected tab on the screen. This allows you to recreate the same state after a rotation or if the app was killed due to low memory.
The things saved in onSaveInstanceState will be forgotten after reboot, and when starting a new instance of an activity they will not be passed, so they are only for remembering the state of the activity
onRetainNonConfigurationInstance
Use this for storing objects which take a long time to load so that you don't have to load them again when the phone is rotated.