Data Reset on Rotation in Android/Monodroid - 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.

Related

The value of the variable changes after onSaveInstanceState is called

As far as I know onSaveInstanceState is called every time the app goes to background. If so then the app might still execute some code while it is in the background. So, I'm wondering what will happen if the state of variable x will be saved during onSaveInstanceState, the app will still continue execution in the background and will change the value of the variable x. A little bit later the process will be killed and the activity will be recreated (when the user will come back to app).
So, what would be the value of x? It seems that it will have an old value(saved during onSaveInstanceState), but what will happen to the new value, will it be lost?
First of all the variables as you call them are not automatically saved you need to put them in a Bundle, similar to when you add Extras to an Intent.
And retrieve those values taking them out of the Bundle in the method onRestoreInstanceState. So the value of a certain variable, when restored would be the one you set in the Bundle if you later change that value and not put it on the Bundle it will be lost.
Also please take into account that this background execution as you called it, it is not so. It is just a minimal code execution to save the state of your activity not an actual background execution

Android app widget SeekBar progress survives onDestroy()

I have started studying Android programming recently after taking a very long break (half a decade) from programming altogether. It's been going well so far.
I have noticed that Seek Bar's progress survives onDestroy event after a rotation (configuration change) happens and this happened on a fragmented activity. Then I created an empty activity and added the widget in the layout and the result is the same (as I expected but still tried due to some code). I also tried overriding the onCreate method and passing a null SavedBundleInstance into super.onCreate() and the result is the same. So I don't know where the Seek Bar's progress location is saved before onDestroy and then passed back unless I am missing some static variable in the SeekBar class or its super classes. (I just thought about that now, I should check it)
Can someone explain why this happens?
Usually, Android Views save their state upon rotation. The same happens for example with EditText. If you input something and then rotate the device, the text will be kept.
It's done inside the specific View class.
But how is this state persisted through orientation changes?
In Activities, for example, you have onSaveInstanceState(Bundle bundle). That method is called by the system before a configuration change occurs. What you do, to persist the state of the Activity, is to store inside bundle the values you want to save. Then, when the Activity is created again, this bundle is passed back to you, for example in the onCreate method. This way you can restore your state.
But what about Views? They use a similar mechanism. If we want the details we have to look at the source code. Let's look at the source code of ProgressBar. It turns out that the View has a onSaveInstanceState, too (this line). You can see there, that the progress is being saved.
I hope this clarifies the mechanism.

Variable values reset after minimizing app

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

Save state for particular duration- android

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."

Camera App Orientation refreshes the parent activity?

I am developing an app with capturing the image as one of the feature. In my home screen i have two spinner. After selecting spinner values user can go capturing the picture by clicking take picture button. Up to this working fine. But the problem resides in Camera App Orientation.
Take Picture button launches the camera.When image get captured it saved(absolutely fine) and came back to the parent activity. But the problems is it refreshes the activity. while coming back i can see following wired things
1.)Some times it shows landscape screen(1second) and back to portait which refreshes the activity and results in resetting the Spinner values.
2.) Some times it just resets the Spinner values.
It's really annoying. I haven't got any clue to get rid this problem. I hope some of you guys will solve this.
Much Appreciated.
It's good to be prepared for anything when you're launching an activity in another app (Camera). The activity could return bad data, the screen orientation could be changed, or it could be a very long time before the user returns to your app. From your description, it sounds as though the orientation is changing every time the Camera app is launched.
Android has built-in state management methods to handle this type of scenario. You can override the onSaveInstanceState() method to store your activity's state (e.g., spinner values), and then restore that state in onCreate(). (Example here.) This will handle the case in which you're launching the Camera app, or your user presses the Home button and then returns to the app, etc.
EDIT:
onRestoreInstanceState() is only called if your activity is killed due to memory pressure and then recreated later. It's much more reliable to use the Bundle passed to onCreate() (which is called again after device rotation) instead. That Bundle will contain everything that was stored in onSaveInstanceState().
If you need to postpone your work until onResume(), you'll want to use onCreate() to populate some member variables in your Activity class, and then make use of those variables in onResume().

Categories

Resources