Android to screen event save data-just one line of code

Source: Internet
Author: User

This afternoon, I got one thing done: Let the system automatically save the data for me on the screen.

The reason for this problem is that the latest project uses full screen, and the view in it changes dynamically. At the beginning, p73 of the power station was used for development, when the lock screen is unlocked, the system can automatically store data for me, but the company will use Samsung for promotion (is it related to our boss who is a Korean doctor ?) Samsung uses a GT-P1010, can not automatically save the data.

Compared with the two tabs, we found that apart from the differences between the two production plants, the system is also different from the domestic version of 2.3, while the South Korean version of 2.2 was initially suspected that 2.2 was not automatically saved, then write a program to verify the idea. To solve the problem, I think the best way is to find the cause. In order to find the cause, I have to find a direction. The first step is to reproduce the problem.

Step 1: Reproduce the problem and write a piece of code:

public class MainActivity extends Activity { static MainActivity mainActivity;String data = "hello";byte[] buf;private  SensorManager mSensorManager;private  Sensor mGravity = null; /** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);mainActivity = this;setContentView(R.layout.main);mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);mGravity = mSensorManager.getDefaultSensor(Sensor.TYPE_ALL);mSensorManager.registerListener(null, mGravity, SensorManager.SENSOR_DELAY_NORMAL);data = "world";System.out.println("mGravity: " + mGravity);//buf = new byte[1024 * 1024];System.out.println("oncreate():" + System.currentTimeMillis());}public static void view2(){mainActivity.setContentView(R.layout.test_2);}@Overrideprotected void onDestroy() {System.out.println("onDestroy():" + System.currentTimeMillis());super.onDestroy();}@Overrideprotected void onStop() {System.out.println("onStop():" + System.currentTimeMillis());super.onStop();}@Overrideprotected void onPause() {System.out.println("onPause():" + System.currentTimeMillis());mSensorManager.registerListener(null, mGravity, SensorManager.SENSOR_DELAY_NORMAL);super.onPause();}@Overrideprotected void onResume() {System.out.println("onResume():" + System.currentTimeMillis());super.onResume();}@Overrideprotected void onRestart() {System.out.println("onRestart():" + System.currentTimeMillis());super.onRestart();}@Overrideprotected void onStart() {System.out.println("onStart():" + System.currentTimeMillis());super.onStart();}@Overridepublic boolean onTouchEvent(MotionEvent event) {if(event.getAction() == MotionEvent.ACTION_DOWN){setContentView(R.layout.test_2);}return true;}}

And then run on two tabs, which sometimes can be saved and sometimes cannot be saved. After several experiments, we found that this is the case.

As long as the screen is landscape screen on the power station, no matter how the screen is unlocked, it is good to save data. Data is always lost when the screen is portrait. The opposite is true for Samsung.

As a result, seven classic State outputs in the Code are added to see what the situation is. The output result is the same as that of the activity. This is the case when the screen is on the TV (this article only discusses unlocking the screen lock)

Pause -- resume does this in the case of a portrait screen:

Press lock: pause -- stop -- destroy -- create -- start -- restart -- resume -- Pause

Press Lock key: Resume

Unlock: pause -- stop -- destroy -- create -- start -- resume

In this case, some operation data on the lock screen is lost, because create or two operations are performed !! In Samsung, there are the same situations, but people are in different directions.

So now there is a solution:

1. Save the data in the onpause method and then reply the data in the onresume method.

2. Let the system automatically save and restore the data (as mentioned above, the data can be saved when the screen is mounted on the TV)

I think I am a lazy person. If I use the first method to save a lot of code, of course this should be the most common practice, after all, this method is recommended for the saving persistent State recommended by Doc. However, I don't want to do these things, because I can save them by the power station, it is only in the case of landscape or landscape screen.

Find out the cause of screen rotation and the impact of the first solution.

Step 1 disable the sensor and use

android:screenOrientation="nosensor"

To set and find that it is still not working-it may be because the sensor is not disabled, so I listened to the event. I thought there was a method called onconfigurationchanged when I looked at the android source code before, so I tried this method, adding override directly in the Code did not directly call it !!!

@Overridepublic void onConfigurationChanged(Configuration newConfig) {System.out.println("onConfigurationChanged: is called " + newConfig);newConfig.orientation = Configuration.ORIENTATION_PORTRAIT;super.onConfigurationChanged(newConfig);}

I am very depressed !! The comment is as follows:

/**     * Called by the system when the device configuration changes while your     * activity is running.  Note that this will <em>only</em> be called if     * you have selected configurations you would like to handle with the     * {@link android.R.attr#configChanges} attribute in your manifest.  If     * any configuration change occurs that is not selected to be reported     * by that attribute, then instead of reporting it the system will stop     * and restart the activity (to have it launched with the new     * configuration).     *      * <p>At the time that this function has been called, your Resources     * object will have been updated to return resource values matching the     * new configuration.     *      * @param newConfig The new device configuration.     */

Then add the onconfichange attribute in the manifest file to the property value of the screen conversion, and then it will be miraculous .!!

Then the system can automatically save the data for me. I have done so many things in one afternoon, and there is only one line of valid code.

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.