1

Every time I turn my phone landscape mode, my activity resets. How do I stop the activity from resetting?

Here's the code I tried using but it does not work.

    @Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
        setContentView(R.layout.get_msg);

    } else {
        setContentView(R.layout.get_msg);
    }
}

manifest file:

android:configChanges="orientation|keyboardHidden|screenSize"

Here is the link where I got it from:

prevent activity restarting when orientation changes

Community
  • 1
  • 1
nothingness
  • 694
  • 3
  • 9
  • 25

3 Answers3

3

change this

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        super.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            System.out.println("this's landscape");

        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            System.out.println("this's portrait");
        }
    }
Karthika PB
  • 1,373
  • 9
  • 16
1

Set android:configChanges="orientation|keyboardHidden|screenSize" in androidMainifest.xml will prevent activity reset,just onConfigurationChanged() would be execute,your reference is handle stuff that support two layouts,it's not fit your application.onConfigurationChanged() you use here reload the layout,so the activity is not reset,just your layout reset.

liuwenzhuang
  • 946
  • 7
  • 14
0

Another way:

String text;
@Override
 protected void onCreate(Bundle savedInstanceState) 
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  int rotation = getWindowManager().getDefaultDisplay().getRotation();
        if(rotation == 0)
            text="Portrait";
        else
            text="Landscape";
}
Rahul Sharma
  • 5,949
  • 5
  • 36
  • 46