I have been looking it up and I just cant seem to wrap myself around the onCreate and Bundles. I understand that the onCreate is called when the program starts but its how the Bundles get passed around and how they are pertinent. Can anyone try to put this into plain english because I cant seem to find it well described.
Thanks
The Bundle in the onCreate method should hold the state of you activity before it was killed.
Simple example, when you change the orientation of your device your activity is recreated.
Imagine the user is filling a long form and he/she accidentally changes the orientation. When the app gets restarted all data entered will be lost unless you persist that information. One possibility is using a Bundle.
If you want to know how to use it, I would recommend that you read this question.
Related
I've implemented onSaveInstanceState and onRestoreInstanceState in my android activity.
These seem to work OK, but I'd like to explicitly save the instance state when certain things happen (e.g. when the user presses a button) in case the application crashes, or I terminate it via the debugger
I can call the method obviously, but that doesn't provide the correct Bundle or anything else.
I've looked around, but I can't see any way to do this. Is it possible?
If not, can anyone offer a decent solution for saving activity state at specific points in case of crash/termination?
Thanks
Bundles are only stored in memory, which means even if you manage to put the data into the Bundle it will be gone if your app crashes.
I'd suggest you to use onSaveInstanceState for what it's designed for - restoring Activity state within an active process. If you want to persist some information so it won't get lost if the app crashes you should use one of the persistent storage options.
Ideally you would have model class for the data that you are displaying. You can persist the model using SQLite and some DAO library. When you do that you can then restore from the database in onCreate of your Activity.
Depending on complexity of the information you can also choose Shared Preferences. If you have only a few key value pairs this is a good choice and you don't have to use models.
Guys i have created an app that updates the text view on button click. It gets the text from a array that i have created. But whenever i close the app and relaunch the app it takes me back to the default text(in my case blank text). Can anyone tell me how to fix this problem. The apk for the App can be downloaded here.
Below i have added the Manifest and Java Files of my project here.
Thanks you very much
Saving the activity state will not help if you want the changes to persist when the activity is finished or killed. You'll need to use some other persistence mechanism. Since it's a simple piece of data, the best solution is probably to save the button text (or its resource ID) in a SharedPreferences file.
You should read up about the Android Activity lifecycle, namely onCreate, onStart, onResume, onPause, onStop and onDestroy.
The main thing you should figure out is when to call the storing and retrieving data method on your choosing such as saving data to and retrieving data from State Bundle or SharedPreferences or even an SQLite Database within the Android Activity lifecycle.
This question and answers here will hopefully solve your problem, but some answers are not the most safe, for example the suggestion of using savedInstanceState.putString() within onSaveInstanceState() and using savedInstanceState.getString() within onRestoreInstanceState() like the accepted answer in the link provided might help depending on what you want to do with your app, however there are some other suggestions in the answer thread as well such as saving to State Bundle and SharedPreferences or even use a SQLite Database as suggested in the thread.
:Saving Activity state in Android
is there a way to prevent activity to be reloaded when navigate back to it for exemple , in case of some activitites who load data from database , it's not a good for users to wait loading data everytime they navigate to this activity.
Short answer regarding the possibility of preventing reload of Activity: no. You should always , at least in my opinion, design your app to handle the Activity lifecycle. The reload (onPause->onDestroy->onCreate->onResume) will also happen when the orientation of the device changes. Having a CPU and/or memory intensive task happen every time the Activity is created would most likely lead to poor user experience.
One solution could be to have an external class handle the database loading and let the class be accessible as a singleton. I know this is discourage by many developers but at the same time I am pretty certain to have read that it is acceptable in many Android specific cases.
Alternatively you could have a class extend Application. Then the Application class could hold on to any session relevant data. This would also give access to the data independent of which Activity is currently shown.
I am sure there is a lot of other options but this is what initially came to mind.
You might want to study the Activity Lifecycle in particular Recreating an Activity
You can save any data that you've gathered or created after displaying the activity for the first time by storing it in a Bundle onSaveInstanceState(Bundle ) method and then recovering it from the same Bundle using the onRestoreInstanceState(Bundle) method.
While invoking the activity set the intent type you want to use. Here is the documentation
For your case FLAG_ACTIVITY_REORDER_TO_FRONT may help.
But on reading your question again, I think you want to keep the data as it is and don't want to load it again. In that case, extend the Application class. Save the data in a variable in this extended class and use it gain from anywhere you want to.
I have a Problem with my Application that when it goes to the background and then return back after 2-3 minutes of inactivity, then Application's on-Create method is called and values are set to the default values, kindly help me to diagnose this Problem and further Application contains a lot of sounds and image-processing in it, may be this fact causes an issue. Thanks
If you did not save your application values in onSaveInstanceState(), they will not be available to you in onCreate(). Save your values in onSaveInstanceState(), and you will be able to get them back in onCreate(). It's as simple as that.
Ok, so quick explanation. I have an app that consists of 3 files.
Main.java, License.java ,App.java
For an example. This app does function 100% except for what i am about to ask. This app does contain a webview and in here lays the issue. I have researched and most everything points to orientation changes. I am not having a problem with orientation because i force landscape at the moment. I am having an issue with the phone going to sleep.
When it wakes, it reloads the page inside the webview. I can add a lock for that, but if someone manually locks their phone, then unlocks it, it will be the same response. Or can you manually lock it if the force wake is in the app? Guess i need to test that one.
Anyways, i am guessing the saveinstancestate will remember the location in the webview and reload to it? If so, which of the oncreates does it go in?
Main is launched when it first opens, this one calls the license, if it passes it then opens the application
Do you do anything special on onPause and onResume? When the phone goes to sleep, as far as I know only onPause should be called, and when woken up onResume. Maybe you load the webview content in onResume?
Also, if you want to share some data amongst your activities, you could try using an Application context instead. I.e. in the AndroidManifest file define a class to be used as application (just like you do with activities, with the "android:name" attribute). The class of course should be a sub-class of "android.app.Application".
See also:
http://developer.android.com/guide/topics/manifest/application-element.html
http://developer.android.com/reference/android/app/Application.html