OnConfigurationChanged () call twice when changing orientation Landscape to portrait

I get a strange problem in my application. The main problem I asked here . android:configChanges="keyboardHidden|orientation" problem android:configChanges="keyboardHidden|orientation" does not work in my code.

so I found a solution to control this @Override onConfigurationChanged() method in my code to control orientation. but the problem is not resolved properly.

Currently, the problem is that onConfigurationChanged() calls twice when we change the orientation of the landscape to the portrait.

If we change the phoneโ€™s portrait to landscape, changing it and working, but now when the user moves the phoneโ€™s landscape to the portrait, then onConfigurationChanged() will call and return the same orientation state, and return the portrait in the second call.

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(); } } 

Journal

 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 

Another query with the same question โ†’

It will kill the current activity when we change orientation (during the call to onConfigurationChanged ). so I have two layouts in another folder according to my previous question. Therefore, when I change the activity of the screen, you delete all the data. How can I save this data to show the user when the user changes the orientation of the phone anyway.

+4
source share
1 answer

I'm not sure how onConfigurationChanged() is called twice, but to avoid erroneous behavior in your application, you can cancel the execution of onConfigurationChanged() if the resulting orientation is now the same as the previous one. Worked for me, hope this works for you too :)

0
source

All Articles