9

how i can stop the restarting or recalling of on create() on screen orientation ,i want to stop the recreation of activity on screen orientation. thanks in advance please tell me any better solution its really creating a problem. like in my program i am selecting some picture but on screen orientation the image goes off so thats why i want to stop the recreation of activity on screen orientation.

enter code here
public void onCreate(Bundle savedInstanceState) 
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainwindow);
    Toast.makeText(getApplicationContext(),"a", 1).show();

    bitmap = (Bitmap)getLastNonConfigurationInstance();
    //Toast.makeText(getApplicationContext(),"a1", 1).show();

    if (savedInstanceState != null) 
    {
        bitmap=BitmapFactory.decodeFile(mImageCaptureUri.getPath());
        Toast.makeText(getApplicationContext(),"preview have value", 1).show();
        preview.setVisibility(View.VISIBLE);
        From_Folder.setVisibility(View.GONE);
        From_Camera.setVisibility(View.GONE);
        preview.setImageBitmap(bitmap);


    }
ravi
  • 177
  • 2
  • 2
  • 11

7 Answers7

31

Up to API 13 there was a new value to the configChanges attribute, screenSize

So if you're using large screens make sure to add screenSize in your configChanges attribute:

    android:configChanges="orientation|keyboardHidden|screenSize"
Parag Chauhan
  • 35,760
  • 13
  • 86
  • 95
Thomas Gatt
  • 849
  • 9
  • 9
11

This is happening because when screen orientation rotates the Activity gets re-started. In this case you can add configChanges attribute in your tag in the AndroidManifest file to stop the re-creation of the Activity.

<activity android:name=".Activity_name"
          android:configChanges="orientation|keyboardHidden">

By, this also it won't stop though the orientation changes.

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
            setContentView(R.layout.login_landscape);
        }
        else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            setContentView(R.layout.login);         
        }
    }
  • Thanks, this was a great solution for maintaining dialogs on orientation change – rwarner Jul 16 '12 at 23:57
  • 1
    in API 13 must incluse also the `screenSize` value to manifest http://stackoverflow.com/questions/11630530/how-to-handle-screen-orientation-changes-when-there-is-an-asyntask-running-with – sabadow Apr 05 '13 at 08:09
5

In your AndroidManifest.xml file, in the activity add

android:configChanges="keyboardHidden|orientation"

Example as below:

 <activity android:name=".YourActivity" android:label="@string/app_name" android:configChanges="keyboardHidden|orientation">
Mohit Verma
  • 3,025
  • 1
  • 20
  • 36
  • 1
    yes it is working after adding these lines to my manifestandroid:configChanges="keyboardHidden|orientation" it is working 100% – ravi Jan 26 '12 at 06:06
3

Not the best, but maybe the easiest solution is to add

android:configChanges="keyboardHidden|orientation" 

to youractivity in your manifest so it looks like

<activity android:name="com.your.activity"
      android:configChanges="keyboardHidden|orientation"/>
mseo
  • 3,881
  • 3
  • 22
  • 26
  • 1
    This is an anti-pattern and must be discouraged. You HAVE to manage the scenario where activity is recreated, since it happens not only when the device is rotated, but when the SO kills activity to release memory. Manage your layout configuration changes and activity recreation using Loaders or ViewModel and Lifecycle components. – Alexander Haroldo da Rocha Mar 17 '18 at 19:05
2

There are a more full list of parameters for prevent activity recreations (in Manifest.xml):

<activity 
    android:name = ".MyActivity" 
    android:configChanges = "orientation|keyboard|keyboardHidden|screenLayout|screenSize">
</activity>
Webest
  • 51
  • 4
0

Two ways for this:

Either you can set android:configChanges="keyboardHidden|orientation" in Manifest file to avoid recreation of activity.

Or Make the changes you want to apply on changing the orientation inside the overrided method

@Override

public void onConfigurationChanged(Configuration newConfig) {

            // Perform the actions

    super.onConfigurationChanged(newConfig);

}
jency
  • 377
  • 2
  • 3
-3

Add

android:orientation="vertical"

or

android:orientation="horizontal"

to your layout in mainwindow.xml.

Example:::

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

No need to add anything in Activity class.

Hope this may help you

sonu thomas
  • 2,161
  • 4
  • 24
  • 38