Simply explain the activity components in Android app by example _android

Source: Internet
Author: User

The activity is the most direct user-accessible component of the Android application that loads the view component to show it to the user and maintains interaction with the user. All activity components need to inherit the activity class, which is an indirect subclass of content that wraps some of the basic characteristics of the activity.

The view component is the base class for all UI components, container components, that is, it can be a layout container, or it can be a basic UI component within a layout container. The view component is typically defined by an XML layout resource file, and the Android system also provides the corresponding implementation classes for these view components. If you need to display the specified view component through an activity, invoke the Setcontentview () method of the activity, which has multiple overloaded methods that can pass an XML resource ID or a View object.

For example

LinearLayout layout=new LinearLayout (this);
Setcontentview (layout);

Or

Setcontentview (R.layout.main);

The activity provides a user interface for Android applications that, when a ac-tivity is opened, has its own lifecycle. The activity classes also provide a corresponding approach to these lifecycles, which can be overridden if you need to respond to different life cycles of the activity. For most business applications, the entire system consists of multiple activity, and in the application the step-by-step navigation jump to open these activity, will form the activity of the rollback stack, the current display and the focus of activity is located in the back of the stack top.

Instance

How to define a number of activity
1. Define a class to inherit activty
2. Call the OnCreate method
3. Register in Anroidmianifest.xml

Second, how to start an activity
1. Generate an Intent object (i.e. intent)
2. Call the SetClass method of the intent object
3. Methods to invoke Startactiviyt in the current Activity inheritance class

Third, the Code
an activity of an XML file

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" 
  xmlns:tools= "http:// Schemas.android.com/tools " 
  android:layout_width=" match_parent " 
  android:layout_height=" Match_parent " 
  android:paddingbottom= "@dimen/activity_vertical_margin" 
  android:paddingleft= "@dimen/activity_ Horizontal_margin " 
  android:paddingright=" @dimen/activity_horizontal_margin " 
  android:paddingtop=" @dimen /activity_vertical_margin " 
  tools:context=". Mainactivity "> 
  <button  
    android:id=" @+id/button1 " 
    android:layout_width=" Match_parent " android:layout_height= "Wrap_content" 
    android:text= "start an activity"/> 
 
</RelativeLayout> 

XML file for the second activity

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= 
"http://schemas.android.com/apk/" Res/android " 
  android:layout_width=" match_parent " 
  android:layout_height=" match_parent " 
  android:o" rientation= "vertical" > 
   
  <textview  
    android:id= "@+id/textview1" android:layout_height= "WRAP_" 
    Content " 
    android:layout_width=" match_parent " 
    android:text=" the second activity " 
    android:gravity=" Center_ Horizontal "/> 
   
</LinearLayout> 

When creating a second XML file is registered in the Androidmianifest.xml file

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android= "http://schemas.android.com/apk/res/" Android "package=" Com.android.xiong.moreactivity "android:versioncode=" 1 "android:versionname=" 1.0 "> android:minsdkversion= "8" android:targetsdkversion= "Uses-sdk"/> <application Android:allowB Ackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/ap Ptheme "> <activity android:name=" com.android.xiong.moreactivity.MainActivity "android:label=" @st 
 
        Ring/app_name "> <intent-filter> <action android:name=" Android.intent.action.MAIN "/> <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity& 
    Gt <activity android:name= "com.android.xiong.moreactivity.SecondActivity" android:label= "Secondactivity" > & Lt;/activity> </apPlication> </manifest> 

 

Start a second activity

Package com.android.xiong.moreactivity; 
Import Android.os.Bundle; 
Import android.app.Activity; 
Import android.content.Intent; 
Import Android.view.Menu; 
Import Android.view.View; 
Import Android.view.View.OnClickListener; 
 
Import Android.widget.Button; 
 
  public class Mainactivity extends activity {private Button button1; 
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
    Setcontentview (R.layout.activity_main); 
    button1= (Button) Findviewbyid (R.id.button1); 
    OnClick onc=new onclick (); 
  Button1.setonclicklistener (ONC); Class OnClick implements onclicklistener{/** * SetClass The parameter of the first method is a context a parameter means you're going to start the act. Ivity is a Class object * context is the parent of the activity/@Override the public void OnClick (View v) {//TODO Aut 
      o-generated method Stub Intent intent=new Intent (); 
      Intent.setclass (Mainactivity.this, Secondactivity.class); MainactIvity.this.startActivity (Intent); }} @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; this adds items to 
    The Action Bar if it is present. 
    Getmenuinflater (). Inflate (R.menu.main, menu); 
  return true; 
 } 


}

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.