Android realizes a sample sharing of page information not being reset when the screen is switched _android

Source: Internet
Author: User

When the screen rotates and switches, the Android mechanism is:
Destroy the current screen activity, and then reopen a new activity that adapts to screen changes.
So how do we get the page information not reset when the screen is switched?

Solution implementation:
1. In the Anroidmainifest.xml activity element, add:

Copy Code code as follows:

Android:configchanges= "Orientation|keyboardhidden"

Or
Copy Code code as follows:

Android:configchanges= "Orientation|keyboard|keyboardhidden"

Indicates that the OnCreate () method is no longer performed when changing the screen orientation, ejecting the software disk, and hiding the soft keyboard.
Instead, it executes onconfigurationchanged () directly.
If this code is not stated, the OnCreate () method is executed once, according to the lifecycle of the activity,
The OnCreate () method usually does some initialization work before it is displayed.

So if you change the screen orientation so that you execute the OnCreate () method, it is possible to create duplicate initialization,
Reducing program efficiency is inevitable and is more likely to result in loss of data due to repeated initialization.
This is a need to avoid!

2. Permission statement:

Copy Code code as follows:

<uses-permission android:name= "Android.permission.CHANGE_CONFIGURATION" ></uses-permission>

The API says that this permission allows us to change the configuration information, but we do not use the permission in the program that changes the screen direction, does it conflict with each other?
Here we can think that when we declare that permission,
The system allows us to capture and modify some configuration information by rewriting the Onconfigurationchanged method in the activity.

3. Rewrite the Onconfigurationchanged method in the activity in the Java source code file:

Copy Code code as follows:

Import android.content.res.Configuration;

The frame callback function onconfigurationchanged from the Android.content.res.Configuration package.
Parameter Newconfig-equipped with new equipment.
When the device configuration information changes (such as changes in the screen direction, the physical keyboard is pushed or closed),
And if an activity is running at this time, the system will call this function.
Note: Onconfigurationchanged will only respond to applications in Anroidmainifest.xml
A change in the configuration type specified by android:configchanges= "Configuration Type";
For other configuration changes, the system destroys activity on the current screen first.
Then reopen a new activity instance that adapts to screen changes.
public void
Onconfigurationchanged (Configuration newconfig)
{
Be sure to call the parent class with the same name function first, and let the framework default function handle the
The following sentence must not be omitted, or it will be thrown: Android.app.SuperNotCalledException exception.
Super.onconfigurationchanged (Newconfig);

To detect the orientation of the screen: portrait or Landscape
if (This.getresources (). GetConfiguration (). Orientation = = Configuration.orientation_landscape)
{
Currently horizontal screen, add extra processing code here
}
else if (this.getresources (). GetConfiguration (). Orientation = = configuration.orientation_portrait)
{
Currently the vertical screen, where additional processing code is added
}

To detect the status of a physical keyboard: launch or close
if (Newconfig.hardkeyboardhidden = = configuration.hardkeyboardhidden_no)
{
The entity keyboard is in the eject state where additional processing code is added
}
else if (Newconfig.hardkeyboardhidden = = Configuration.hardkeyboardhidden_yes)
{
The entity keyboard is in a closed state where additional processing code is added
}
}

Create a new activity and print each lifecycle:
First step:
Run the activity and get the following information:

Copy Code code as follows:

OnCreate
OnStart
Onresume

Step Two:
Press CRTL + F12 to switch Cheng:

Copy Code code as follows:

Onsaveinstancestate
OnPause
OnStop
OnDestroy
OnCreate
OnStart
Onrestoreinstancestate
Onresume

Step Three:
Then press CRTL + F12 to switch to the vertical screen, the discovery printed two times the same information:

Copy Code code as follows:

Onsaveinstancestate
OnPause
OnStop
OnDestroy
OnCreate
OnStart
Onrestoreinstancestate
Onresume
Onsaveinstancestate
OnPause
OnStop
OnDestroy
OnCreate
OnStart
Onrestoreinstancestate
Onresume

Fourth Step:
Modify the activity elements in the Androidmanifest.xml file,
Add android:configchanges= "Orientation",
Press CRTL + F12 to toggle Cheng: (same as no changes above)

Copy Code code as follows:

Onsaveinstancestate
OnPause
OnStop
OnDestroy
OnCreate
OnStart
Onrestoreinstancestate
Onresume

Fifth Step:
And then press CRTL + F12 to switch to the vertical screen,
Discovery does not print the same information again,
But a line of onconfigchanged is printed more than one:

Copy Code code as follows:

Onsaveinstancestate
OnPause
OnStop
OnDestroy
OnCreate
OnStart
Onrestoreinstancestate
Onresume
Onconfigurationchanged

Sixth step:
Modify the activity elements in the Androidmanifest.xml file,
Put

Copy Code code as follows:

android:configchanges= "Orientation"

Change into
Copy Code code as follows:

Android:configchanges= "Orientation|keyboardhidden"

When you press CRTL + F12 to switch Cheng,
Just print onconfigchanged:

Copy Code code as follows:

Onconfigurationchanged

Seventh Step:
When you press CRTL + F12 to switch to the vertical screen:

Copy Code code as follows:

Onconfigurationchanged
Onconfigurationchanged

Second, Summary:
1. When the android:configchanges of the activity element in the Androidmanifest.xml file is not set,
The screen will recall each life cycle, cut the horizontal screen will be executed once, cut the vertical screen will be executed two times;

2. Set the activity element in the Androidmanifest.xml file.
When android:configchanges= "orientation",
Cut screen or will be called back to each life cycle, cut horizontal, vertical screen will only be executed once;

3. Set the activity element in the Androidmanifest.xml file.
When android:configchanges= "Orientation|keyboardhidden",
The cut screen does not recall each lifecycle, only the Onconfigurationchanged method is executed!

Third, add one point:
1. The current Activity generation event pops up Toast and Alertdialog, and the life cycle is not changed!

2.Activity Press the home key at run time (same as being completely covered):

Copy Code code as follows:

Onsaveinstancestate--> onpause--> onStop
Onrestart--> OnStart--> onresume

3.Activity is not fully overwritten just loses focus:

Copy Code code as follows:

OnPause--> Onresume

Related Article

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.