Activity of Android four components

Source: Internet
Author: User

1. Overview

Activity is one of the four major Android components (application component), simply speaking, activity is the usual user interface, in general, an activity occupied by the window is full screen, But it can also be a small floating window on top of other activity. An Android project has at least one activity,activity that can have multiple view instances, such as text boxes, progress bars, checkboxes, buttons, and so on. Multiple activity through intent to achieve jumps.

[1] Create a new activity

(1) to inherit (extends) the activity class, the replication OnCreate (bundle) method, and then bind this activity's layout file XML through "Setcontentview ()".

public class Secondactivity extends Activity {    @Override protected void onCreate (Bundle savedinstancestate) {        TODO auto-generated Method Stub super.oncreate (savedinstancestate); Setcontentview (r.layout.secondactivity);//Bind layout file}}

(2) The newly added activity must also be registered within the Androidmanifest.xml.

<activity       android:label= "second"       android:name= ". Secondactivity"   //activity is located. Java class  >< /activity>

2, intent class to achieve the jump between the screen:

[1] Explicit jump: requirements must know the package and class of the activated component.

Scenario: Jump inside the application. Disadvantage: High coupling.

General use Method:

(1) in Layout "Activity_main.xml" To add a button ID btn, and then create an arbitrary layout (will jump to the layout), I named second.

(2) Add "Secondactivity.jar" as the target activity in the Java package as follows:

Package Com.myandroid;import Android.app.activity;import Android.os.bundle;import android.view.menu;import Android.view.menuitem;public class Secondactivity extends Activity  {@Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.second);}}

(3) Add a new activity tag to androidmanifest, at least declare a android:name attribute, as follows:

<activity android:name= ". Secondactivity ">  

(4) To add a jump in the start activity, import android.content.Intent is required, and the namespace is as follows:

Package Com.myandroid;import Android.app.activity;import Android.os.bundle;import android.view.view;import Android.view.menu;import Android.view.menuitem;import Android.widget.button;import Android.content.Intent;public Class Mainactivity extends Activity {    @Override    protected void onCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Button button = (button) This.findviewbyid (R.ID.BTN); Button.setonclicklistener (new View.onclicklistener () {    @ Override public    void OnClick (View v) {Intent Intent = new Intent ();    Instantiate Intent class Intent.setclass (Mainactivity.this, secondactivity.class);    Jump StartActivity (Intent);    Start finish ();//Stop the current activity, and if not, press the return key to jump back to the original activity    }        );}    }

The other two modes of display are:

/*** Method One ***/
Intent intent1 = new Intent ();
The first parameter is the current package name, the second parameter is the class name of the jump activity, and the package name must be added
Intent1.setclassname ("Com.myandroid.intent", "com.myandroid.intent.SecondActivity");
StartActivity (INTENT1);

/*** Method Two ***/
The first parameter is the context, the second parameter is the class of jump activity Intent Intent2 = new Intent (this, secondactivity.class);

[2] implicit intent: only need to know the action and data jump activity, you can activate the corresponding component.

Scenario: When you do not know the class name of an application, the package name, and the reference is not. You can jump to other apps. The implementation of the browser with the parameter access. Advantages: Low coupling.

Note: Data and type cannot be written separately, using the Setdataandtype () method for general use:

(1) Add a new activity tag in androidmanifest, to activate activity with implicit intent, set the action, category, data three parameters, action and category names can be defined by themselves, As long as the setting of the intent in the Openotheractivity method is consistent, it can be found by the filter and eventually activated. As follows:

<activity android:name= ". Secondactivity ">         <intent-filter>             <action android:name=" com.myandroid.SecondActivity "/>             <category android:name= "Com.myandroid.Default"/>             <data                     android:host= "Cn.itcast.demo"                     Android:scheme= "Itcast"/>        

(2) The settings for intent are as follows.



As long as the action name is the same as the action name in the filter, you can match it, and if the match succeeds, you can view the category and data
If there are no categories and data, then only the action will be matched

Intent.addcategory ("Com.android.Default");
StartActivity (Intent) for intent object injected a Android.intent.category.DEFAULT category, you must add the category, or error can not find activity;

Operation found error:

Cause: startactivity (Intent) injected a android.intent.category.DEFAULT category for the intent object, it must be added, otherwise the error cannot find the activity;

Resolution: Add the corresponding <categoryandroid:name= "Android.intent.category.DEFAULT" in the corresponding activity in Androidmanifest Intent-filter "/> category.

At this time to run the program, the discovery is also possible, can jump successfully.

Conclusion: As long as the action and category in the intent settings appear in the Intent-filter, it is possible to jump successfully, provided the data parameters are not set.

3. Intent data transfer

Scenario: Passing data, RP Calculator.

Bundle: Data Bundle, Package transfer

[1] Way one:

First page

Intent inent = new Intent ();

Intent.setclass (This,secondactivity.class);

Bundle bundle = new bundle (); Package Send

Bundle.putstring ("name", name);

Intent.putextra ("Maps", bundles);

StartActivity (Intent);

A second page

TextView TV = new TextView (this);

Intent Intent = This.getintent ();

Bundle bundle = Intent.getbundleextra ("Maps"); Get Packaged Data bundle

String name = bundle.getstring ("name"); Take out the data you need

Tv.settext (name);

Setcontentview (TV);

[2] The way two:

Create object class (JavaBean) person

Implementing the Serializable interface

Private static final long serialversionuid = 1L;

First page

Intent inent = new Intent ();

Intent.setclass (This,secondactivity.class);

person person = new person (name); Serialization of object delivery

Intent.putextra ("person", person);

StartActivity (Intent);

A second page

TextView TV = new TextView (this);

Intent Intent = This.getintent ();

person p = [person] Intent.getserializableextra ("person");

String name = P.getname ();

4. Intent return Data

[1] activity that provides the return value:

Will StartActivity (intent);       Changed to Startactivityforresult (intent,1000); 1000 is the request code. Understood as identity

Implementing the Onactivityresult Method

if (Requestcode = = && ResultCode = = ACTIVITY.RESULT_OK)

{

String Rptext = Data.getstringextra ("Rptext");

Toast.maketext (this,rptext,1). Show ();

}

[2] Activity to receive data:

Implement Onbackpressed method//Return key Click event

Remove the inherited parent class method, because clicking the back key after inheriting will return directly

Intent data = new Intent ();

Data.putextra ("Rptext", text);

Setresult (Activity.result_ok,data); Change the return code to 0

This.finish (); That is, return events

5. The life cycle of the activity is switched between the screen and the

By default, activity is reloaded and data is easily reloaded, resulting in data loss. such as games.

Specific process: OnPause, OnStop->ondestory, onCreate->onstart->onresume
Avoid switching between the screens and the screen re-create activity join in the activity node of the manifest file: android:configchanges= "Orientation|keyboardhidden"

6. Task Stack

To improve the user experience an application that contains multiple activity stacks at the top of the activity is displayed in the interface to get the focus.

The features of the task stack are: Advanced post-out, LIFO.

7. Activity components in Androidmanifest

The Androidmanifest file contains the activity component of the filter as the default boot component, which is automatically invoked by the system when the application starts.

Activity of Android four components

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.