Activity is one of the four components of Android, and activity can be seen as a screen where users can do some things. Activity is usually full screen, but can also be smaller than the screen and floating on other windows, such as the activity of the style set 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, rewrite the OnCreate method in this class and add the layout file (Setcontentview) to the method, initializing the control (Findviewbyid).
Finally, the activity needs to be declared in the androidmanifest.xml.
If you want to set the activity as the entry for the program, you need to set intent-filter for that activity
1 <!--Entrance Statement -2 <Intent-filter>3 <ActionAndroid:name= "Android.intent.action.MAIN" />4 5 <categoryAndroid:name= "Android.intent.category.LAUNCHER" />6 </Intent-filter>
Start a new activity we can use startactivity this method
1 New Intent (This, secondactivity. Class); 2 Intent.putextra ("Type", type); 3 StartActivity (Intent);
The above code indicates that I want to open a secondactivity and pass a data to it in the past.
Sometimes we need to open other applications, and at this point we can't do it using the above method.
We need to open it in an implicit intnet way.
1 New Intent (intent.action_call); 2 uri uri = uri.parse ("tel:110"); 3 Intent.setdata (URI); 4 startactivity (intent);
The above code indicates: Dial 110.
Note: Dial-up permission is required for dialing
<android:name= "Android.permission.CALL_PHONE"/>
If our application is called by another application, we need to find the activity that can be recalled in Androidmanifest.xml, define a intent-filter in this node, set our own action
1 < intent-filter > 2 android:name = " Com.zy.android.firstandroid " /> 3 < category android:name = "Android.intent.category.DEFAULT" /> 4 Intent-filter >
Once set up, we can access the activity in the following ways in other applications
1 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, and we can't get the returned data if we use StartActivity.
Android offers us another way to meet this need
Replacing startactivity with Startactivityforresult
At the same time, we need to rewrite the Onactivityresult callback method in the activity that receives the data. When the new activity returns data, we can get the data in this method
1 @Override2 protected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {3 //TODO auto-generated Method Stub4 Super. Onactivityresult (Requestcode, ResultCode, data);5 //Requestcode Request Code6 //ResultCode Result Code7 //The data returned by the new activity is stored here and can be taken out for use8}
The life cycle of activity is not in this place to introduce you.
Next we describe the status of saving activity
We know that when the status of the activity is onpause,onstop, it is possible to kill the system, if the system is killed, the user returns to see a new interface, but the user does not know that the system killed the previous activity, Users will feel very strange, eh, I just the operation? Where did it go? Where did it go?
If such a thing really happens, it will be a very unfriendly application!
To avoid this situation, 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, and then do the data save operation in the method, and save it in the bundle.
1 @Override 2 protected void onsaveinstancestate (Bundle outstate) {3 // TODO auto-generated Method Stub 4 Super . Onsaveinstancestate (outstate); 5 Outstate.putstring ("name", "User action data"); 6 }
Where do we pick it up after we save it? We can fetch this data in 2 places.
Careful garden friends will find such a problem, onCreate this method has a parameter, why we have not used it? In fact, this parameter is the data we just saved, after the system kill, if we do the data save, then the next time we open the activity, we will bring this data together.
So, we can get the data in the OnCreate method.
1 @Override2 protected voidonCreate (Bundle savedinstancestate) {3 //TODO auto-generated Method Stub4 Super. OnCreate (savedinstancestate);5 Setcontentview (r.layout.result_layout);6 //get the data saved before system kill7 if(savedinstancestate!=NULL){8String name = savedinstancestate.getstring ("name");9 }Ten OneTv_info =(TextView) Findviewbyid (r.id.tv_info); ABtn_email =(Button) Findviewbyid (r.id.btn_email); -Btn_phone =(Button) Findviewbyid (r.id.btn_phone); - the Btn_email.setonclicklistener (click); - Btn_phone.setonclicklistener (click); - -}
We can also rewrite onrestoreinstancestate () This callback method, in this method can also get to the system killactivity before the data saved
Follow me to learn android-activity introduction