Method to solve the problem of data loss in Android horizontal and vertical screen switching

  • 2021-07-18 08:58:57
  • OfStack

When the Android screen is switched, the current Activity will be destroyed, and then all the data on the Activity will be lost.
For example, checkbox of each item above Listview, when the horizontal and vertical screens are switched, all selected information is lost in the check box.

Solution 1: Add android: configChanges= "orientationkeyboardHidden" in the activity tab of Androidmanifest. xml


<activity 
   android:configChanges="orientation|keyboardHidden" 
   android:label="sqltest" 
   android:name=".SqlData" 
   
  </activity> 

Solution 2: Forcibly limit activity in horizontal screen state or vertical screen state, and prevent activity from switching between horizontal and vertical screens
landscape for horizontal screen
portrait stands for vertical screen


<activity 
   
   android:label="sqltest" 
   android:name=".SqlData" 
   android:screenOrientation="landscape" > 
  </activity> 

Solution 3: In Activity, through replication of onConfigurationChanged method, the processing mode in different screen states is realized.


@Override 
 public void onConfigurationChanged(Configuration newConfig) { 
  // TODO Auto-generated method stub 
  super.onConfigurationChanged(newConfig); 
  /* 
   *  Horizontal and vertical screen detection  
   */ 
  if (this.getResources().getConfiguration().orientation 
 
  == Configuration.ORIENTATION_LANDSCAPE) { 
 
   //  It is currently a horizontal screen  
   // Implementation code  
 
  } 
 
  else if (this.getResources().getConfiguration().orientation 
 
  == Configuration.ORIENTATION_PORTRAIT) { 
 
   //  It is currently a vertical screen  
   // Implementation code  
 
  } 
 
  /* 
   *  Physical keyboard state detection  
   */ 
 
  if (newConfig.hardKeyboardHidden 
 
  == Configuration.HARDKEYBOARDHIDDEN_NO) { 
 
   //  The physical keyboard is pushed out  
   // Implementation code  
 
  } 
 
  else if (newConfig.hardKeyboardHidden 
 
  == Configuration.HARDKEYBOARDHIDDEN_YES) { 
 
   //  The physical keyboard is closed  
   // Implementation code  
 
  } 
 
 } 

author: conowen @ Big Clock

Original address: http://blog.csdn.net/conowen

The above is the whole content of this article, hoping to help everyone learn Android software programming.


Related articles: