Android Studio Development Note Five

Source: Internet
Author: User
Tags home screen

1.activity: An application component that provides a user interface to interact with the program
2.android Four components:
--activity--service--broadcastreceiver--content Provider
3. How to create a usage activity
(1) Acivity classes that inherit Android
(2) Override method
(3) Setting the display layout
(4) in the Androidmanifest file, register the activity
4.activity life cycle: Create---> Destroy
OnCreate () Create OnStart () run Onresume () Get focus
OnPause () lost Focus onStop () pause OnDestroy () Destroy Onrestart ()
OnCreate ()->onstart ()->onresume ()->onpause ()->onstop ()->ondestroy ()
Four states of 5.activity:
(1) Active status: Activity at the top of the interface, get focus
(2) Pause state: Activity loses focus, but visible to user, program pauses
(3) Stop state: activity is completely obscured but retains all status and member information
For example, back to the home screen, the program runs in the background
(4) Inactive: activity is stopped


Intent
1. Intent to assist in the completion of communication between various components of Andro
2. Implement the Jump mode:
(1) >startactivity (Intent)--no return result
(2) >startactivityforresult (Intent,requestcode)
--there is a return result, that is, two pages can be exchanged for data
>onactivityresult ()
>setresult ()
Code:
Manifest file:
<?xml version= "1.0" encoding= "Utf-8"?>
<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
Package= "Com.example.administrator.myintent" >
<application
Android:allowbackup= "true"
android:icon= "@mipmap/ic_launcher"
Android:label= "@string/app_name"
Android:supportsrtl= "true"
Android:theme= "@style/apptheme" >
<activity android:name= ". Mainactivity ">
</activity>
<activity android:name= ". Factivity ">
The following code, which represents this as the first page
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>
<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name= ". Sactivity ">
</activity>

</application>

</manifest>


The code for the first page:
public class Factivity extends Activity {
Private Button bt1;
Private Button BT2;
Private TextView TV;
@Override
protected void OnCreate (Bundle saveinstancestate) {
Super.oncreate (saveinstancestate);
Setcontentview (r.layout.factivity);
/*
Implement a jump between pages by clicking BT1
1.startactivity Way to achieve
1.> Initialize Intent
*/
bt1= (Button) Findviewbyid (R.id.button1_first);
Bt2= (Button) Findviewbyid (R.id.button2_first);
tv= (TextView) Findviewbyid (R.ID.TEXTVIEW1);
Bt1.setonclicklistener (New View.onclicklistener () {
/*
First parameter: Context this
Second parameter: Destination file
*/
@Override
public void OnClick (View v) {
Intent intent=new Intent (factivity.this,sactivity.class);
StartActivity (Intent);
}
});
/*
Implemented through Startactivityforresult
*/
Bt2.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
Intent intent=new Intent (factivity.this,sactivity.class);
/*
The first parameter is the intent parameter
The second parameter is an identity of the request, which is an integer type
*/
Startactivityforresult (intent,1);
}
});
}
/*
Jump through Startactivityforresult, accept the returned data
First parameter: The identity of the request
Second parameter: The identity returned by the second page, that is, to know which page to return from.
Third parameter: Data returned from the second page
*/
@Override
protected void Onactivityresult (int requestcode,int resultcode,intent data) {
Super.onactivityresult (Requestcode,requestcode,data);
if (requestcode==1&&resultcode==2) {
String Content=data.getstringextra ("data");
Tv.settext (content);
}
}
}
The code for the second page:
public class Sactivity extends Activity {
Private Button BT;
Private String content= "This is the data returned by the second page";
@Override
protected void OnCreate (Bundle hinstace) {
Super.oncreate (Hinstace);
Setcontentview (r.layout.sactivity);
/*
When does the second page begin to return data?
The data that is passed back to the first page is actually a intent object
*/
bt= (Button) Findviewbyid (R.id.button1_second);
Bt.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
Intent data=new Intent ();
Data.putextra ("Data", content);
Callback Data
Setresult (2,data);
End Current Page
Finish ();
}
});
}
}

Android Studio Development Note Five

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.