umar
umar

Reputation: 3133

Android web app gets restart

I am facing problem in building android webview . The problem is that when the app is running and phone change direction , i mean from horizontal to vertical or vice versa the app get restarted. Thanks

Upvotes: 0

Views: 426

Answers (6)

JAL
JAL

Reputation: 3319

umar... Saving instance state is quite different on the Android. On a soft kill (phone rotation) you may save your non view state in onSaveInstanceState using bundles. On a hard kill (back button while activity has focus) you may elect to save your non view and view state in onStop perhaps using preferences. You can restore your state in onCreate.

You can leverage the fact that IF onSaveInstanceState is called it will be called BEFORE onStop. So this lets you set a flag isSavedInstanceState to true in onSaveInstanceState to avoid saving prefs in onStop except on a hard kill. The trick is to reset the flag isSavedInstanceState to false in onResume NOT in onCreate.

JAL

I have sample code here.

Upvotes: 0

E.Z. Hart
E.Z. Hart

Reputation: 5757

See also: Activity restart on rotation Android

Upvotes: 1

dbyrne
dbyrne

Reputation: 61081

The default behavior is to restart the activity when the screen orientation changes. You can write custom code to handle orientation change events yourself though:

  • Add android:configChanges="orientation" to your AndroidManifest.xml
  • Override onConfigurationChanged from your activity

Upvotes: 2

Yoni Samlan
Yoni Samlan

Reputation: 38075

Another possibility (usually a decent fit for lighter Activities that don't have state outside a WebView, for instance) is to absorb the rotation event and let the view redraw itself. See http://www.androidguys.com/2008/11/11/rotational-forces-part-three/ - the idea is:

  1. Put an android:configChanges entry in your file, listing the configuration changes you want to handle yourself versus allowing Android to handle for you.

  2. Implement onConfigurationChanged() in your Activity, which will be called when one of the configuration changes you listed in android:configChanges occurs

Upvotes: 1

Will Tate
Will Tate

Reputation: 33509

Umar,

You will want to add the android:configChanges="orientation" parameter to your Activity in your AndroidManifest.xml to prevent your activity from restarting on orientation change.

See: http://developer.android.com/guide/topics/manifest/activity-element.html#config

Upvotes: 1

Captain Spandroid
Captain Spandroid

Reputation: 247

The default android behaviour is to destroy and recreate the activity on orientation change. You can either override onSaveInstanceState() to save your application data before destroy, or you can call onRetainNonConfigurationInstance() to keep hold of a stateful object. See the android docs.

Upvotes: 1

Related Questions