How to disable onCreate action when changing orientation?

How to disable the onCreate event when I rotate my device from portrait orientation? Because when the application starts, it will receive most of its data from the Internet, and will not download this data well every time the user switches his device.

+8
java android
source share
6 answers

In manifest activity tag

Android: configChanges = "orientation | Screen size"

Example:

<activity android:name=".MainActivity" android:configChanges="orientation|screenSize"> 
+18
source share

Use the check on onCreate checkbox. Make flag = true during initialization / declaration add android: configChanges = "orientation" in ur manifest file

In the ur java file, override the onConfigurationChanged method and make the flag false.

After that ur onCreate will be called, but the mention of code in if will not be called. Move ur code inside if condition.

try using this.

 static boolean flag = true; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); if(flag) Log.d("ONCREATE", "flag is true"); } @Override public void onConfigurationChanged(Configuration newConfig) { // TODO Auto-generated method stub Log.d("ONCONFIGCHANGE", "CALLED" ); flag = false; super.onConfigurationChanged(newConfig); } 
+3
source share

An alternative approach is to use the onSaveInstanceState method to store any persistent data in the Bundle. The state must be restored either in onRestoreInstanceState or in onCreate . In onCreate you need to analyze the savedInstanceState parameter, and if it is not equal to zero, you must restore the previously saved state.

+3
source share

This solution, of course, is the best working. in the manifest file add

 <activity android:configChanges="keyboardHidden|orientation|screenSize" android:name="your activity name" android:label="@string/app_name" android:screenOrientation="landscape"> </activity 

And in your activity class add the following code

 @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { //do your stuff here // } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { //do your stuff here } } 
+2
source share

In manifest.xml add android: configChanges = "orientation" for activity.

+1
source share

For C # Xamarin, I have this solution:

 using System.Collections.Concurrent; public static class ObjectExchanger { private static ConcurrentDictionary<string, object> ObjectList = new ConcurrentDictionary<string, object>(); /// <summary> /// Add key and object. Do not add the same key twice. /// </summary> /// <param name="key"></param> /// <param name="obj"></param> public static void Add(string key, object obj) { ObjectList.GetOrAdd(key, obj); } /// <summary> /// Get object via key. Do net Get twice. Key/object will remove after get it once. /// </summary> /// <param name="key"></param> public static object Get(string key) { object obj = null; ObjectList.TryRemove(key, out obj); return obj; } } 

You can use the OnCreate method to backup and restore your data for activity after rotation.

-one
source share

All Articles