7

i am getting strange issue in my app. The main issue that i have asked here. issue is android:configChanges="keyboardHidden|orientation" is not working in my code.

so i have found the solution to manage it by @Override onConfigurationChanged() method in my code to manage orientation. but yet the issue is not solve properly.

Currently issue is that onConfigurationChanged() is calling twice when we change orientation landscape to portrait.

If we change phone portrait to landscape its changing and working but now when user move the phone landscape to portrait then onConfigurationChanged() will call and return same orientation state & in second call it will return portrait.

Code :

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

        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
        {
             Log.e("On Config Change", "LANDSCAPE");

            Toast.makeText(getApplicationContext(), "L", Toast.LENGTH_LONG)
                    .show();
        } else 
        {
            Log.e("On Config Change", "PORTRAIT");
            Toast.makeText(getApplicationContext(), "P", Toast.LENGTH_LONG)
                    .show();
        }
    }

Log

first mode its port mode , so change in land mode 

02-28 12:10:06.274: E/On Config Change(540): LANDSCAPE 
02-28 12:10:14.154: E/On Config Change(540): LANDSCAPE
// here after changed the land mode try to chage in port mode then its calling two times as you can see as per the log 
02-28 12:10:14.593: E/On Config Change(540): PORTRAIT
02-28 12:11:39.524: E/On Config Change(540): LANDSCAPE

One more query with same question >>

It will kill the current activity when we change the orientation (at time of calling onConfigurationChanged). so i have two layouts in different folder as per my previous question.So when i change the screen activity will remove all data.so how can i save that data to show user when user change the phone orientation in any case.

Community
  • 1
  • 1
Chintan Khetiya
  • 15,962
  • 9
  • 47
  • 85

1 Answers1

0

I am not sure how onConfigurationChanged() gets called twice, but to avoid buggy behavior in your app you can void the execution of onConfigurationChanged() if the orientation received now is same as the previous orientation. Worked for me, hope it works for you too :)

Scott
  • 1,863
  • 2
  • 24
  • 43
Rc N
  • 103
  • 3
  • 6
  • android:configChanges="keyboardHidden|orientation|screenSize" onConfigurationChanged() is called twice with different parameters, once for orientation and once with some screenSize params in our case. You can check why is onConfigurationChanged is called twice by printing Configuration parameter in logs. – Rc N Apr 20 '17 at 11:22