Introduction to android-Activity, android-activity

Source: Internet
Author: User

Introduction to android-Activity, android-activity

Activity is one of the four main components of android. activity can be viewed as a screen. You can perform some operations here. The activity is usually full screen, but it can also be smaller than the screen and floating on other windows. For example, you can set the activity style to dialog.

How to create an Activity

First, we need to create an interface (layout) for this activity)

Then, we need to create a new class, which is a subclass of Activity.

Next, override the onCreate method in this class, load the layout file (setContentView) in this method, and initialize the control (findViewById ).

Finally, declare the activity in androidmanifest. xml.

If you want to set the activity as the program entry, you need to set intent-filter for the activity

1 <! -- Entry declaration --> 2 <intent-filter> 3 <action android: name = "android. intent. action. MAIN "/> 4 5 <category android: name =" android. intent. category. LAUNCHER "/> 6 </intent-filter>

To start a new activity, we can use startActivity.

1         Intent intent = new Intent(this,SecondActivity.class);2         intent.putExtra("type", type);3         startActivity(intent);

The code above indicates that I want to open a SecondActivity and pass a data to it.

Sometimes, we need to open other applications. At this time, we cannot use the above method.

We need to use the implicit Intnet method to open

1 Intent intent = new Intent(Intent.ACTION_CALL);2 Uri uri = Uri.parse("tel:110");3 intent.setData(uri);4 startActivity(intent);

The code above indicates: call 110.

Note: The dialing permission is required.

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

 

If our application is called by other applications, we need to run it in androidmanifest. find the activity that can be invoked in xml, define an intent-filter in this node, and set our own action

1  <intent-filter >2         <action android:name="com.zy.android.firstandroid"/>3         <category android:name="android.intent.category.DEFAULT"/>4  </intent-filter>

After setting, we can access this activity in other applications in the following ways:

1 Intent intent = new Intent("com.zy.android.firstandroid");2 startActivity(intent);

Sometimes, we don't just start a new activity. We may need to receive the data returned by the new activity. If startActivity is used, we cannot obtain the returned data.

Android provides us with another way to meet this requirement.

Use startActivityForResult to replace startActivity

At the same time, we need to rewrite the onActivityResult callback method in the activity that receives data. When the new activity returns data, we can obtain the data in this method.

1 @ Override2 protected void onActivityResult (int requestCode, int resultCode, Intent data) {3 // TODO Auto-generated method stub4 super. onActivityResult (requestCode, resultCode, data); 5 // requestCode Request Code 6 // resultCode result code 7 // data the data returned by the new activity is stored here, which can be retrieved using 8}

The lifecycle of an activity is not described here.

Next we will introduce how to save the activity status.

We know that when the activity is in the onPause or onStop status, it may be killed by the system. If it is killed by the system, the user will return, I see a brand new interface, but the user does not know that the system kill the previous activity, and the user will feel very strange. What was my operation just now? Where? Where have you gone?

If this happens, it will be a very unfriendly application!

To avoid this problem, we need to save some data before the system kill the activity.

Activity provides us with a callback method calledonSaveInstanceState()

We can rewrite this method, save the data in this method, and save it to the Bundle.

1 @ Override2 protected void onSaveInstanceState (Bundle outState) {3 // TODO Auto-generated method stub4 super. onSaveInstanceState (outState); 5 outState. putString ("name", "user operation data"); 6}

 

After saving it, where can we get it? We can get the data in two places.

You can find such a problem. The onCreate method has a parameter. Why have we never used it? In fact, this parameter is the data we just saved. After the system is killed, if we save the data, the system will bring this data to us next time we open this activity.

So, we can get data in the onCreate method.

1 @ Override 2 protected void onCreate (Bundle savedInstanceState) {3 // TODO Auto-generated method stub 4 super. onCreate (savedInstanceState); 5 setContentView (R. layout. result_layout); 6 // obtain the data saved before the system kill. 7 if (savedInstanceState! = Null) {8 String name = savedInstanceState. getString ("name"); 9} 10 11 TV _info = (TextView) findViewById (R. id. TV _info); 12 btn_email = (Button) findViewById (R. id. btn_email); 13 btn_phone = (Button) findViewById (R. id. btn_phone); 14 15 btn_email.setOnClickListener (click); 16 btn_phone.setOnClickListener (click); 17 18}

We can also override the onRestoreInstanceState () callback method. In this method, we can also obtain the data saved before the system killactivity.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


In Android, how can the functions of Activity and View be distinguished?

I often encounter a situation where some of the functions are in activity and some of the functions are in view. How can I define the set/get or public data for data sharing between the two.

Android interview introduces the activity lifecycle,

Description of the main life cycle of an activity: onCreate (Bundle savedInstanceState): called when an activity is created. This method also provides Bundle-based access to any previously stored status!
OnStart (): called when the activity is visible to the user on the screen.
OnResume (): called when the activity starts to interact with the user (whether it is to start or restart an activity, this method is always called ). OnPause (): This method is called when the activity is paused or the cpu and other resources are withdrawn. It is used to save the activity status and protect the site. Press the stack!
OnStop (): called when the activity is stopped and converted into an invisible stage and subsequent lifecycle events.
OnRestart (): called when the activity is restarted. The activity is still in the stack, rather than starting a new activity. OnDestroy (): called when the activity is completely removed from the system memory. This method is called. 2. the lifecycle of the activity during horizontal/vertical screen switching.
1. Create an Activity and print the lifecycle of the Activity.
2. Run the Activity and obtain the following information:
OnCreate --> onStart --> onResume -->
3. When switching to landscape screen by pressing crtl + f12
OnSaveInstanceState --> onPause --> onStop --> onDestroy --> onCreate --> onStart --> onRestoreInstanceState --> onResume -->
4. When you press crtl + f12 to switch to the portrait screen, the same log is printed twice.
OnSaveInstanceState --> onPause --> onStop --> onDestroy --> onCreate --> onStart --> onResume --> onSaveInstanceState --> onPause --> onStop --> onDestroy --> onCreate --> onStart --> onRestoreInstanceState --> onResume -->
5. Modify AndroidManifest. xml, add android: configChanges = "orientation" to the Activity, and perform step 3.
OnSaveInstanceState --> onPause --> onStop --> onDestroy --> onCreate --> onStart --> onRestoreInstanceState --> onResume -->
6. Execute Step 4 again and find that the same information will not be printed, but an onConfigChanged line will be printed.
OnSaveInstanceState --> onPause --> onStop --> onDestroy --> onCreate --> onStart --> onRestoreInstanceState --> onResume --> onConfigurationChanged -->
7. Change android: configChanges = "orientation" in step 5 to android: configChanges... the remaining full text>

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.