As I say in the topic, I want my WebView to prevent from reloading the webpage when another activity comes to the foreground or just when the orientation is changed. The reason why is because the WebView content is generated by Javascript/AJAX almost completely. After searching on a couple of forums, I found out many people suggested to use "saveState" and "restoreState" methods, but when I look the documentation, it says:
Please note that this method no longer restores the display data for this WebView. See savePicture(Bundle, File) and restorePicture(Bundle, File) for saving and restoring the display data.
So, here I was using that savePicture and restorePicture as it follows:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
... some other lines ....
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
if (savedInstanceState == null){
loadInicialWebUi();
}else{
mWebView.restoreState(savedInstanceState);
boolean restoredPic = mWebView.restorePicture(savedInstanceState, new File("savedDisplayWebView.temp"));
Log.d(TAG, "restored Picture:" + restoredPic);
}
}
#Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
mWebView.saveState(savedInstanceState);
boolean savedPic = mWebView.savePicture(savedInstanceState, "savedDisplayWebView.temp");
Log.d(TAG, "saved Picture:" + savedPic);
super.onSaveInstanceState(savedInstanceState);
}
And well, those logs were revealing that it saves the picture, but it couldn't restore it. I suspect there might be something about the File references, but I couldn't think of a better way of getting the reference of the File I created when saving the state.
Anyone thrilled?
I would appreciate any clues/suggestions.
Thanks in advance.
Manuel.
I would load the page in onCreate() and put this attribute on your Activity in the manifest:
android:configChanges="orientation|keyboardHidden"
This will prevent your Activity from being restarted when orientation changes. And since your Activity loads the page in onCreate(), bringing another Activity to the foreground won't cause you to reload the page.
Related
I'm moving from activity 1 to activity 2 and send some data via intent .
It's ok and no problem ,but when I leave activity 2 and then come back , the intent is empty . For solving this problem I've tried this code :
#Override
protected void onSaveInstanceState (Bundle outState) {
outState.putInt("row_id", row_id);
}
#Override
protected void onRestoreInstanceState (Bundle savedInstanceState) {
int row_idss = savedInstanceState.getInt("row_id");
Log.v("this","restore" + Integer.toString(row_idss));
}
I tried to save it before I leave the activity and retrieve it on re-open .anyway , It didn't work and didn't log it either.
I tried onPause and onResume , strangely they didn't call either . I used this code to make sure it runs
#Override
protected void onResume() {
super.onResume();
Log.v("this","test");
}
nothing logged .
Could you help me to solve this problem ? I don't know what should I do to solve it
thanks so much
You are missing the calls:
super.onSaveInstanceState(outState);
super.onRestoreInstanceState(savedInstanceState);
Check the answer of this question, in this answer they're not using the method onRestoreInstanceState but just the onCreate to restore the data. You also could use onRestoreInstanceState if you need a different behaviour.
Im developing an app that download tweets from twitter.
When my app change orientation, the activity loads again the tweets (it downloads again the same tweets).
So whats the best solution for a case like this ?
Is using android:configChanges="orientation|keyboardHidden|screenSize" a way to go ? I have read that is a bad practice.
Thanks
You can override onSaveInstanceState and onRestoreInstanceState to avoid downloading same tweets again.
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putSerializable(<Key>, <Value>);
super.onSaveInstanceState(savedInstanceState);
}
When the orientation is changed, restore the saved tweets.
[Edit]
Alternatively, you can do it without overriding onRestoreInstanceState:
public void onCreate(Bundle savedInstanceState) {
if (savedInstanceState!= null) {
value = savedInstanceState.getSerializable(<Key>);
}
}
More information here.
I have looked in all the site and I don't understand why it's not working on my side.
Please find my source code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
Log.e("EVERYTHING", "OK");
}
}
and
#Override
public void onSaveInstanceState(Bundle outState) {
outState.putString("test", "titi");
Log.e("Save", "titi");
super.onSaveInstanceState(outState);
}
I launch my appli, then press the home button, then restart my phone and launch the appli again, but the savedInstanceState is null.
Could you please help me.
Regards
onSaveInstanceState is used to persist state in memory so it's not suitable for situations like you described above. If you restart your phone, obviously all state store not persistently (only in memory) is not preserved.
If you want to store state this way, you should look at something persistent, e.g. shared preferences or SQLite.
I have an activity with action bar tab. Each tab contain a fragment. Now when I rotate my device, bundle in my corresponding fragment is coming as null. This is taken care when I using device post android 3.2, but it is happening when device is Andoird3.0. I am having a headache after working on this issue. I crossed check various link on SO, but no help. Although I have given enough details, still will provide some code snippet as at various cases user ask for code snippet.
In my fragment class I am storing this value
#Override
public void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
outState.putBoolean("textboxVisible", true);
}
this is storing one boolean variable which it retrived as below.
/**
* Function called after activity is created. Use this
* method to restore the previous state of the fragment
*/
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null)
{
//restore the state of the text box
boolean textboxVisible = savedInstanceState.getBoolean("textboxVisible");
if (textboxVisible)
{
//do some stuff
}
}
}
but after rotation savedInstanceState is coming as null.
I don't what is going wrong. I have read in some document that below 3.2 the onCreateView() of
fragment is not called with bundle value. But to deal with this. Any help will be appreciated.
if you use setRetainInstance(true) the savedInstance bundle is always gonna be null after orientation changed. SO you cannot really save something with it, but what you can do if you need to save something, is to put it in a data member of the fragment, because setRetainInstance(true) preserves the fragment and doesn't destroy it, so after the device was rotated you gonna have the same values.
Try to get the savedInstanceState in onCreate of the Fragment.
Like
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
if (savedInstanceState != null) {
// IT MUST NOT BE NULL HERE
}
}
Please try... i hope it will work
I am using Intents to switch between activities in my Android app. I am putting data in the Intent for use in the next activity. When I switch the phone between landscape and portrait modes, the values passed from the intent are lost and I get a NullPointerException.
Can someone please tell me what could be wrong.
There's a lot of code to post it entirely. But if someone needs to look at specific parts of code, I can post it here.
Edit
I solved the issue of state not being saved. But another problem I faced is that none of the buttons on the screen work after the orientation has been changed. On button press, I get this warning in LogCat
02-25 23:07:49.190: WARN/WindowManager(58): No window to dispatch pointer action 0
Please help.
When you switch orientation the activity is recreated and onCreate is recalled so you have to use the bundle to save your current state and restore after an orientation change. You can see this in action if you have just an app with a TextView and you enter text and change orientation. If you bundle your state for onCreate you can curb this. This is probably also why you have a NullPointer after the orientation changes. It is annoying as all hell but something we have to live with.
This link on a series of orientation tutorials and this first one in particular should help you understand exactly what is going on and how to successfully maintain your current state.
Update: There is also a post on SO Activity restart on rotation Android that deals with almost the same thing.
Edit for follow up question:
Did you re-attach your click handlers after the orientation change?
Write this in your manifest file..in which activity you want this--
android:configChanges="orientation|keyboardHidden"
Edited: Use this one for new APIs versions--
android:configChanges="orientation|keyboardHidden|screenSize"
Definitely it will work..
Try this:
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(SOME_KEY, "blah blah blah");
}
#Override
public void onCreate(Bundle savedInstanceState) {
...
somevalue = savedInstanceState.getString(SOME_KEY);
...
}
It possible to declare an attribute android:configChanges with the value of "orientation", this will prevent the activity from being restarted. Instead, the activity remains running and its onConfigurationChanged() method is called.
Declare < android:configChanges="orientation|keyboardHidden"/> in your manifest. This allows you manage the change of Orientation/Keyboard visibility by yourself. Of course, You don't need to override the callback method for manage it.
Hi I also encountered this problem.
what fixed it for me was:
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the user's current game state
savedInstanceState.putString("Username", mUsername);
savedInstanceState.putString("Password", mPassword);
savedInstanceState.putString("UserID", mUserID);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
and then in onCreate():
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
mUsername = "?";
mPassword = "?";
mUserID = "?";
} else {
mUsername = extras.getString("Username");
mPassword = extras.getString("Password");
mUserID = extras.getString("UserID");
}
} else {
mUsername = (String) savedInstanceState.getSerializable("Username");
mPassword = (String) savedInstanceState.getSerializable("Password");
mUserID = (String) savedInstanceState.getSerializable("UserID");
}
then you can be sure the objects are not null.