The activity repeatedly enters the onDestroy onCreate method.

Source: Internet
Author: User

Problem: If pad is flat on the table and there is no operation, the activity will repeatedly go to The onDestroy onCreate method.

Android: configChanges = "keyboardHidden | orientation" is added with this attribute. All right, but I have not switched the screen horizontally or vertically.

The 180-degree gravity sensor Code new MySensorEventListener (this). register () is registered in the activity. However, no 180-degree switching is performed.

So the problem is okay, but the exact source of the problem is unclear.


1. MySensorEventListener 180-degree gravity sensing class

Package cn. zhl. util; import android. app. activity; import android. content. pm. activityInfo; import android. hardware. sensor; import android. hardware. sensorEvent; import android. hardware. sensorEventListener; import android. hardware. sensorManager; public class MySensorEventListener {Activity activity; public MySensorEventListener (Activity activity) {this. activity = activity;} public void register () {// gravity sensor listener Se NsorEventListener lsn = new SensorEventListener () {public void listener (Sensor s, int accuracy) {}@ Overridepublic void onSensorChanged (SensorEvent e) {// TODO Auto-generated method stubfloat x, y, z; x = e. values [SensorManager. DATA_X]; y = e. values [SensorManager. DATA_Y]; z = e. values [SensorManager. DATA_Z]; TurnScreen (x, y, z) ;}}; SensorManager sensorMgr = (SensorManager) activity. getSystemServ Ice (activity. SENSOR_SERVICE); Sensor sensor = sensorMgr. getdefasensensor (Sensor. TYPE_ACCELEROMETER); // register the gravity sensor listener. The third parameter is the detection accuracy sensorMgr. registerListener (lsn, sensor, SensorManager. SENSOR_DELAY_UI);}/*** based on the gravity sensing detection signal, the screen is flipped 180 degrees ** @ param x * x axis value,-10 ~ 10 * @ param y * y axis value,-10 ~ 10 * @ param z * z axis value,-10 ~ 10 */private void TurnScreen (float x, float y, float z) {// if (activity. getRequestedOrientation () = ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE & y <-5) {activity. setRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_REVERSE_LANDSCAPE);} // The Reverse else if (activity. getRequestedOrientation () = ActivityInfo. SCREEN_ORIENTATION_REVERSE_LANDSCAPE & y> 5) {activity. setRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE);} // else if (activity. getRequestedOrientation () = ActivityInfo. SCREEN_ORIENTATION_PORTRAIT & x> 5) {activity. setRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_REVERSE_PORTRAIT);} // The Reverse else if (activity. getRequestedOrientation () = ActivityInfo. SCREEN_ORIENTATION_REVERSE_PORTRAIT & x <-5) {activity. setRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_PORTRAIT );}}}

2,

Android: configChanges = "keyboardHidden | orientation" is not used in AndroidManifest. xml files. Of course, it is useful.

If this attribute is configuredThis function is called directly during screen switching.The onConfigurationChanged method in the onCreate method does not re-execute the onCreate method.If this attribute is not configured, The onCreate method will be re-called. The following is a test

Package com. test; import android. app. activity; import android. content. res. configuration; import android. OS. bundle; import android. widget. editText; import android. widget. textView; public class TestActivity extends Activity {EditText et; TextView TV; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); et = (EditText) findViewById (R. id. et); TV = (TextView) findViewById (R. id. TV); System. out. println ("I am onCreate method");} @ Override public void onConfigurationChanged (Configuration newConfig) {super. onConfigurationChanged (newConfig); if (newConfig. orientation = Configuration. ORIENTATION_LANDSCAPE) {TV. setText ("Landscape");} else {TV. setText ("portrait ");}}}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.