I try to save and restore the state of 3 WebViews in my activity by using onSaveInstanceState() and the restoreState() method of my WebView in onCreate(). This works just fine when I had only one WebView, but with 3 WebViews to save, it looks like it's only the last one save which counts and overrides the other saves; and so when I use the restoreState() method in onCreate(), the first WebView has the content of the third WebView and the 2 others are empty.
Here is a sample of my activity code:
#Override
public void onSaveInstanceState(Bundle outState) {
((WebView)findViewById(R.id.webview1)).saveState(outState);
((WebView)findViewById(R.id.webview2)).saveState(outState);
((WebView)findViewById(R.id.webview3)).saveState(outState);
}
#Override
public void onCreate(Bundle savedInstanceState) {
((WebView)findViewById(R.id.webview1)).restoreState(savedInstanceState);
((WebView)findViewById(R.id.webview2)).restoreState(savedInstanceState);
((WebView)findViewById(R.id.webview3)).restoreState(savedInstanceState);
}
Should I use some trick to use another bundle for each view? Have I done something wrong somewhere else in my code that causes this or is it the standard behaviour?
Your 2nd call of saveState() overwrites the contents saved in the first call and the 3rd call overwrites the content of the 2nd call. At the end of the execution of onSaveInstanceState() the bundle only contains the 3rd WebView's state.
This is why when you restore the state of the 3rd WebView gets restored to the 1st WebView.
Create a new bundle object of each of your WebViews and put them into the outState Bundle object using this giving a different key for each Bundle:
public void putBundle (String key, Bundle value)
In onCreate() get the Bundles based on the 3 keys and restore the states.
Related
I have a Fragment F attached on Activity A. When another activity becomes front-activity it is called onSaveInstanceState. I've overrided that function and it looks like:
public void onSaveInstanceState(Bundle outState){
saveState_to_outstate
}
Now, when the Fragment F is front again it is called onViewCreated(View view, Bundle savedInstanceState). I would like to restore previously saved state but I cannot because savedInstanceState is null though it was written before in onSaveInstanceState.
Why it happened?
If you want to restore Fragment F with previous Data. As according to my knowledge you should perform inside onViewStateRestored() method of Fragment.
OnViewStateRestored (Bundle) tells the fragment that all of the saved state of its view hierarchy has been restored.
you Should visit this link for your reference:Fragment
Method A: You can pass the data from Activity A to Fragment F by bundle or intent. it's similar to usage of saving instance.
Send data from activity to fragment in android
MethodB: You can only use following function with onCreate() at the same place, either fragment F or Activity A.
public void onSaveInstanceState(Bundle outState){
outState.putInt("STATE_NAME", 123);//123 is the data you gonna save
super.onSaveInstanceState(outState);//Don't forget to put this
}
MethodC: Use hardcode-like SharePreference Android Shared preferences example ,it's also like the bundle and intent, and it won't be effected by Activity or fragment life periods.
I would like my application to save state after it already has loaded once. For example in one of my activities I have a ListView. If the user scrolls it, and than switches activities, I wish for them to go back to the ListView activity and have the same scrolling position. I noticed that pressing the back button goes back to a saved version of the state. This is the exact kind of save I want (where it saves the state of the previous activity). Except I want to do this from anywhere in the application, not just when the back button is pressed... Please help me.
You have to override onSaveInstanceState(Bundle savedInstanceState) and store the values you want to save in Bundle object as name value pair.
#Override public void onSaveInstanceState(Bundle savedInstanceState)
{
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putBoolean("position", 12);
savedInstanceState.putString("Name", "John");
}
also you have to override onRestoreInstanceState() where you'd extract the values:
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
boolean myPosition=savedInstanceState.getBoolean("position");
String name= savedInstanceState.getString("Name");
}
see this link for help LINK
It can be done with the saveInstanceState() and onRestoreInstanceState() methods. The first one called near onPause(), the second called before onResume(). To save the state of a view, call onSaveInstanceState() from view.
For example, listview.onSaveInstnceState();
Here's a detailed article
https://futurestud.io/blog/how-to-save-and-restore-the-scroll-position-and-state-of-a-android-listview
I am storing some data in static varibles in Activity1 and accessing in Activity3,and Activity 5. ie..
Activity1---> Activity2--->Activ3
.....................|
......................Activity4.-----> Activ5
This works fine if we close the application completely, from Activity1 (ie if the user is at Activ5 if he clicks back button then -->Activ4-->Activ2-->Activ1-->Exit)
But the user is exiting app at Activ3,4,5 by clicking Mobile exit button(Not the application exit), Now after few hrs the user is reopening application then , It(app) gets started from Activi3 or 4 or 5. (ie where ever app was closed).
Now, Since i am using some data(which i stored in static varibles in Activ1.)
I am getting null values. Why this is happining. How to avoid this types of errors.
I have used sharedpref to avoid this.Is this the only solution ?
Restore the state of activity when it is recreated, so that the values passed can be retrieved at a later time.
e.g. for an integer that was passed through intent do as following: -
//this will save the value if an activity is killed in background.
#Override
protected void onSaveInstanceState(Bundle outState)
{
getIntent().putExtra("count", getIntent().getStringExtra("count"));
super.onSaveInstanceState(outState);
}
//In restore instance state, retrieve the stored values. The following work can also be done //in oncreate, as when an activity is killed in background, onCreate method is also called.
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
if(savedInstanceState == null)
return;
int count = getIntent().getIntExtra("count", 0);
super.onRestoreInstanceState(savedInstanceState);
}
You need to add onSaveInstanceState methods to your earlier activities, and check the bundle received by the onCreate methods. Check out the Activity Lifecycle for details.
You should not store values in static members, the activity context gets released, thus you losing your static values. The preferred way of passing values between activities is using Bundles along with the Intents.
you can create new class and extend application ,and in that store all data that you want, its very useful but remember if you do that you must add name of your application in manifest file
I have a tabhost with 3 tabs. In each of these tabs there is a webiview.
When i click a tab the webview need to "reload" even when i have been there before, it have not been saved.
Is there any way to save the webview ?
This can be handled by overrwriting onSaveInstanceState(Bundle outState) in your activity and calling saveState from the webview:
#Override
protected void onSaveInstanceState(Bundle outState) {
webView.saveState(outState);
super.onSaveInstanceState(outState);
}
Then recover this in your onCreate after the webview has been re-inflated of course:
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.blah);
WebView webview = (WebView)findViewById(R.id.webview);
if (savedInstanceState != null)
webview.restoreState(savedInstanceState);
else
webview.loadUrl(URLData)
}
restoreState was never reliable. And come 2015 the documents accept the fact. (The change was probably made to the documents around 2014).
This is what it says now.
If it is called after this WebView has had a chance to build state
(load pages, create a back/forward list, etc.) there may be
undesirable side-effects. Please note that this method no longer
restores the display data for this WebView.
And the corresponding entry for saveState() speaks thus:
Please note that this method no longer stores the display data for
this WebView.
What you really should do inside the onCreate method is to call webView.loadUrl() if you want to display the last visited url, please see this answer:
If you are concerned about the webview reloading on orientation change etc.
you can set your Activity to handle the orientation and keyboardHidden changes, and then just leave the WebView alone
Alternatively you can look into using fragments with your tabHost. when entering another webView you can set fragment to hide and the other to show. when you return to the previous fragment the content will not have been destroyed. Thats how i did it and it worked for me.
I make a login form with dynamic field validation. I have 3 fields username, email & password & all of these field are required. When field length = 0, I set error
editText.setError( getText(R.string.cannot_be_blank) );
and this code works fine, but when I change the orientation, all the errors disappear
How to save error state?
Thanks.
When the orientation is changed the framework will recreate the Activity by calling onCreate(Bundle savedInstanceState). Before the switch in orientation the onSaveInstanceState(Bundle outState) method will be called if it is overridden in your Activity.
You can save the state of your errors in the Bundle passed into the onSaveInstanceState method. This bundle is passed to your onCreate() method as the savedInstanceState Bundle.
Therefore you need to override the onSaveInstanceState method in your Activity as follows (saving the state of your errors):
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean("errorOccurred", errorState);
super.onSaveInstanceState(outState);
}
Then in your onCreate method check if the savedInstateState Bundle is null or not. If not, you can retrieve the values out of it with the following code:
boolean errorOccurred = false;
if (savedInstanceState != null) {
errorOccurred = savedInstanceState.getBoolean("errorOccurred");
}
When the orientation is changed the Android framework destroys the Activity and then creates a new one for the new orientation. So all your state is lost.
Use SharedPreferences to store and restore your state and TextEdit values.
What happens when you turn the device is that your Activity runs through its lifecycle in order to deal with the fact that the layout must change from portrait to landscape.
You should take a look at the developer docs on how to Handle Runtime Changes.