Android improves Activity + Intent in Article 4

Source: Internet
Author: User

Android has three basic components: Activity, Service, and BroadcastReceiver. They are all started with Intent. This article describes the lifecycle of an Activity and its Intent usage.

In the previous example, Activity is always used. When a Layout XML is bound with an Activity, it can be regarded as a Form. If multiple Layout XML files are bound with an Activity, it is an Application. Intent can be divided into explicit Intent and implicit Intent: explicit Intent is used to start clear target components (three major components mentioned above ), multiple Activity calls within the same Application are also explicit Intent; implicit Intent means that there is no clear target component called, either a system or a third-party program. Implicit Intent is generally used to call the functions of system components. Related routines are easily found on the Network (you need to apply for permissions when calling some system components ).

The running status of Acitivity is divided into onCreate, onDestroy, onStart, onStop, onRestart, onResume, onPause. onCreate corresponds to onDestroy, onStart corresponds to onStop, and onResume corresponds to onPause.

First paste this article to run:

 

 

When Acitivity1 is switched to Activity2, the status of Acitivity1 changes. If finish () is used, onDestroy () is triggered ().

 

The status of Acitivity2 changes from Activity2 to Activity1. The process is onCreate ()-> onStart ()-> onResume (), and the destroy method is onPause ()-> onStop () -> onDestroy (). In addition, to add a second Activity to the project, you must add Activity2 to AndroidManifest. xml-> Application.

Source code of main1.xml:

View plaincopy to clipboardprint?
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<Button android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: id = "@ + id/main1.Button01"
Android: text = "Jump to Activity2"> </Button>
<EditText android: text = "@ + id/EditText01" android: id = "@ + id/EditText01"
Android: layout_width = "wrap_content" android: layout_height = "wrap_content"> </EditText>
<Button android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: id = "@ + id/main1.Button02"
Android: text = "Jump to external Activity"> </Button>
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<Button android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: id = "@ + id/main1.Button01"
Android: text = "Jump to Activity2"> </Button>
<EditText android: text = "@ + id/EditText01" android: id = "@ + id/EditText01"
Android: layout_width = "wrap_content" android: layout_height = "wrap_content"> </EditText>
<Button android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: id = "@ + id/main1.Button02"
Android: text = "Jump to external Activity"> </Button>
</LinearLayout>
 

Source code of main2.xml:

View plaincopy to clipboardprint?
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout android: id = "@ + id/LinearLayout01"
Android: layout_width = "fill_parent" android: layout_height = "fill_parent"
Xmlns: android = "http://schemas.android.com/apk/res/android">
<Button android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: id = "@ + id/main2.Button01"
Android: text = "Return Activity1"> </Button>
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout android: id = "@ + id/LinearLayout01"
Android: layout_width = "fill_parent" android: layout_height = "fill_parent"
Xmlns: android = "http://schemas.android.com/apk/res/android">
<Button android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: id = "@ + id/main2.Button01"
Android: text = "Return Activity1"> </Button>
</LinearLayout>
 

Source code of Activity1:

View plaincopy to clipboardprint?
Package com. testActivityIntent;
Import android. app. Activity;
Import android. content. Intent;
Import android. content. SharedPreferences;
Import android.net. Uri;
Import android. OS. Bundle;
Import android. util. Log;
Import android. view. View;
Import android. widget. Button;
Import android. widget. EditText;
Public class testActivityIntent extends Activity {
/** Called when the activity is first created .*/
Button btnToInternalActivity;
Button btnToExternalActivity;
EditText tbBundle;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
Log. e ("Activity1", "onCreate"); // displays the current status. onCreate corresponds to onDestroy.
SetContentView (R. layout. main1 );

BtnToInternalActivity = (Button) this. findViewById (R. id. main1_Button01 );
BtnToExternalActivity = (Button) this. findViewById (R. id. main1_Button02 );
BtnToInternalActivity. setOnClickListener (new ClickEvent ());
BtnToExternalActivity. setOnClickListener (new ClickEvent ());
TbBundle = (EditText) this. findViewById (R. id. EditText01 );
}
Public void onDestroy ()
{
Super. onDestroy ();
Log. e ("Activity1", "onDestroy"); // displays the current status. onCreate corresponds to onDestroy.
}
@ Override
Public void onStart ()
{
Super. onStart ();
Log. e ("Activity1", "onStart"); // display the current status, onStart

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.