user1023177
user1023177

Reputation: 641

How to disable onCreate action when will orientation be changed?

How can I disable onCreate event when I turn my device from portrait orientation? Because when application starts it will get much of its data from the Internet, and it will be bad to download this data every time the user turns his device.

Upvotes: 12

Views: 10353

Answers (6)

nxexoxn
nxexoxn

Reputation: 1

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 in in OnCreate Method to backup and restore your data for the activity after rotation.

Upvotes: -1

Shailendra Singh Rajawat
Shailendra Singh Rajawat

Reputation: 8242

In Activity tag of manifest write

android:configChanges="orientation|screenSize"

example:

<activity android:name=".MainActivity"
            android:configChanges="orientation|screenSize">

Upvotes: 31

A Raj
A Raj

Reputation: 139

This solution is by far the best working one. in your 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

    }
}

Upvotes: 2

slkorolev
slkorolev

Reputation: 6001

Alternative approach is usage of onSaveInstanceState method for saving any non persistent data into a Bundle. The state should be restore either in onRestoreInstanceState or in onCreate. In onCreate you have to analyse savedInstanceState parameter and if it's not null then you should restore previously saved state.

Upvotes: 3

Hardik4560
Hardik4560

Reputation: 3220

Use a flag check in onCreate. Make flag=true at the time of initialization/declaration add android:configChanges="orientation" in ur manifest file

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

After doing so ur onCreate will be called but the code mention in if won't be called. Move ur code inside the 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);
}

Upvotes: 3

Kannan Suresh
Kannan Suresh

Reputation: 4580

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

Upvotes: 1

Related Questions