Personal Interpretation of Activity 2

Source: Internet
Author: User

In this section, let's take a look at configuration changes (such as screen orientation, language switching, or input device changes!


As we can see from the official documentation, when the configuration changes, any content displayed on the user interface needs to be updated, the safest way is to close the previous instance after the configuration changes, then obtain the resources of the previous instance, create a new instance, and restore it to the status of the previous instance!

Here, we will change the configuration and use the simplest screen reversing test. The simulator reversing can be implemented using Ctrl + F11! We know that a running Activity will restart when the configuration file changes. We will test it based on this idea. Here we use the above example to print onCreate, onStop!

Step 1: Create a new project: ChangeConf

Step 2: Modify the ChangeConf. java file. The Code is as follows:

Step 3: run the program. The following characters are printed in Log:

 

Step 4: Change the screen direction when the program is running again. You can see that the Log prints the following content:

From the printed content above, you can clearly see the words configure changed! You can also see that the Activity is indeed like this, first shut down and then restart!


Of course, not all cases require a restart when the configuration changes. We can declare android in the activity of the AndroidManifest file: the configChanges attribute defines a special processing method for special configuration change types. When this configuration change event occurs, the system does not take the default restart action, instead, the onConfigurationChanged (Configuration) method of the current Activity is called for processing.

We will continue to test the above example here, and still process the screen turning to this configuration change event.

Step 1: Modify the AndroidManifest file of the above project. The modification is as follows:


<Activity
Android: label = "@ string/app_name"
Android: name = ". changeconfacti.pdf"
Android: configChanges = "orientation | keyboardHidden">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
 
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
<Activity
Android: label = "@ string/app_name"
Android: name = ". changeconfacti.pdf"
Android: configChanges = "orientation | keyboardHidden">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>

<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity> Step 2: Modify the java source file. The Code is as follows:
Private static final String mark = "ChangeConf ";


@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Log. I (mark, "onCreate ");
}
 
@ Override
Protected void onStart (){
Super. onStart ();
Log. I (mark, "onStart ");
}
 
@ Override
Protected void onRestart (){
Super. onRestart ();
Log. I (mark, "onRestart ");
}
 
@ Override
Protected void onResume (){
Super. onResume ();
Log. I (mark, "onResume ");
}
 
@ Override
Protected void onPause (){
Super. onPause ();
Log. I (mark, "onPause ");
}
 
@ Override
Protected void onStop (){
Super. onStop ();
Log. I (mark, "onStop ");
}
 
@ Override
Protected void onDestroy (){
Super. onDestroy ();
Log. I (mark, "onDestroy ");
}
 
@ Override
Public void onConfigurationChanged (Configuration newConfig)
{
Super. onConfigurationChanged (newConfig );
Log. I (mark, "not restart! ");
}
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Log. I (mark, "onCreate ");
}
Www.2cto.com
@ Override
Protected void onStart (){
Super. onStart ();
Log. I (mark, "onStart ");
}

@ Override
Protected void onRestart (){
Super. onRestart ();
Log. I (mark, "onRestart ");
}

@ Override
Protected void onResume (){
Super. onResume ();
Log. I (mark, "onResume ");
}

@ Override
Protected void onPause (){
Super. onPause ();
Log. I (mark, "onPause ");
}

@ Override
Protected void onStop (){
Super. onStop ();
Log. I (mark, "onStop ");
}

@ Override
Protected void onDestroy (){
Super. onDestroy ();
Log. I (mark, "onDestroy ");
}

@ Override
Public void onConfigurationChanged (Configuration newConfig)
{
Super. onConfigurationChanged (newConfig );
Log. I (mark, "not restart! ");
} Step 3: run the program and switch the screen orientation. You can see that the Log prints the following content:

It means it's exactly what we said before. Well, come here first!

From the column chenlong12580
 

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.