2

i developed a simple Android App with using jquerymobile and Phonegap. Problem is that, after change rotation, is app completely reloaded. Is possible to prevent this reload?

Thanks very much for any advice.

redrom
  • 11,502
  • 31
  • 157
  • 264

5 Answers5

4

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.

Community
  • 1
  • 1
Nik
  • 2,380
  • 2
  • 16
  • 15
  • 1
    FYI there is now the concept of a "Fragment" in Android, available in Android 3.0, that supplant this approach: http://developer.android.com/guide/topics/fundamentals/fragments.html – esilver Nov 14 '11 at 06:35
  • Link update: http://android-developers.blogspot.co.uk/2009/02/faster-screen-orientation-change.html – Onimusha Mar 11 '13 at 00:20
2

Add this

android:configChanges="keyboardHidden|orientation"

into the activity node of your manifest.

Hyperboreus
  • 31,997
  • 9
  • 47
  • 87
1

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.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
Leonel Galán
  • 6,993
  • 2
  • 41
  • 60
  • 1
    This doesn't work for me under Cordova 2.7.0 and Android 4.0.3. Same as Cordova 2.3.0. The web page is fully-restarted and then crashes on the classic JNI local reference table overflow error as data is somehow duplicated..(another problem I don't have an answer for) – Rob Von Nesselrode May 08 '13 at 07:12
0

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.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Chetan Bhoyar
  • 501
  • 4
  • 12
0

Putting this code in AndroidManifest.xml helps:

android:configChanges="keyboardHidden|orientation|screenSize"

Credit goes to this article:

https://github.com/phonegap/build/issues/159

Dũng Trần Trung
  • 6,198
  • 3
  • 24
  • 20