i developed a simple Android App with using jquerymobile and Phonegap. Proble开发者_如何学Pythonm is that, after change rotation, is app completely reloaded. Is possible to prevent this reload?
Thanks very much for any advice.
It depends whether you want to stay in portrait mode, or whether you do want to switch orientation, but reload faster.
If you want to stay in portrait mode irrespective of the orientation of the device then look at (for example) How do I disable orientation change on Android?.
If you want to change orientation then you can't get away from the fact that your activity will be restarted in the new orientation. That's how Android works.
What you can do is cache the data your app needs just before the activity is killed, and then reload that cached data as soon as the activity is restarted in the new orientation, significantly speeding up the process. For that you need onRetainNonConfigurationInstance()
, and this article -- http://developer.android.com/resources/articles/faster-screen-orientation-change.html -- explains how to do that.
Add this
android:configChanges="keyboardHidden|orientation"
into the activity node of your manifest.
So this is a phonegap issue and after searching a little bit more:
Add
android:configChanges="orientation|keyboardHidden"
parameter to the Activity tag (AndroidManifest.xml
) that inherits from DroidGap.
Orientation will still work, but the webpage won't be refreshed.
Please override this two methods to prevent your web page ( activity ) from reload and do write this code of line android:configChanges="orientation|screenSize"
in your web view activity in Manifest.xml file.
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
//Save the state of Webview
mWebView.saveState(savedInstanceState);
super.onSaveInstanceState(savedInstanceState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore the state of webview
mWebView.restoreState(savedInstanceState);
}
where mWebView is your webview object.
Putting this code in AndroidManifest.xml helps:
android:configChanges="keyboardHidden|orientation|screenSize"
Credit goes to this article:
https://github.com/phonegap/build/issues/159
精彩评论