Android is a simple example of how to call another Activity in an Activity?

Source: Internet
Author: User

In the article "jump between mobile phone pages", I introduced how to use setContentView () to switch the Layout file for jump between mobile phone pages. This is suitable for changing the background and text color, text content, but it is always an Activity. If you want to hand over the main control permission to another Activity, you cannot do it by switching the Layout file Layout.

How can I hand over the master control to another Activity? Principle: The Intent object and startActivity () method are used together. The startActivity () method is used in the main program to call another Activity. However, the most important thing is not the startActivity () method, but the Intent object. Intent indicates "Intent" in English, in the main Activity, tell the program what it is and where it wants to go. This is what the Intent object is going to do. Place a button in the main Activity (ActivityOne). When you click this button, tell the main Activity (ActivityOne) to go to ActivityTwo and place one in ActivityTwo to return to the main Activity (ActivityOne).

Steps

I. layout file writing

1.1: The first layout file main. xml

<? 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"
>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "I'm ActivityOne"
/>
<Button
Android: id = "@ + id/mainbtn"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "ActivityTwo call"
/>
</LinearLayout>

1.2: The second layout file two. xml

<? 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"
>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "I'm ActivityTwo"
/>
<Button
Android: id = "@ + id/twobtn"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "ActivityOne call"
/>
</LinearLayout>

Ii. Code File writing

2. 1: MainActivity. java

Package com. menglin. callactivity;

Import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;

Public class MainActivity extends Activity
{
// Declare a Button Object
Private Button mainbtn = null;
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
// Load the main. xml layout File
SetContentView (R. layout. main );
// Obtain the Button object using the findViewById () method
Mainbtn = (Button) findViewById (R. id. mainbtn );
// Bind the Button object to a click listener event
Mainbtn. setOnClickListener (listener );
}

// Listen for events
Private OnClickListener listener = new OnClickListener ()
{
@ Override
Public void onClick (View v)
{
// Create an intent object
Intent intent = new Intent ();
// Specify the original class and the class to be started
Intent. setClass (MainActivity. this, TwoActivity. class );
// Call another new Activity
StartActivity (intent );
// Close the original Activity
MainActivity. this. finish ();
}
};
}

2. 2: TwoActivity. java

Package com. menglin. callactivity;

Import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;

Public class MainActivity extends Activity
{
// Declare a Button Object
Private Button twobtn = null;
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
// Load the two. xml layout File
SetContentView (R. layout. two );
// Obtain the Button object using the findViewById () method
Twobtn = (Button) findViewById (R. id. twobtn );
// Bind the Button object to a click listener event
Twobtn. setOnClickListener (listener );
}

// Listen for events
Private OnClickListener listener = new OnClickListener ()
{
@ Override
Public void onClick (View v)
{
// Create an intent object
Intent intent = new Intent ();
// Specify the original class and the class to be started
Intent. setClass (TwoActivity. this, MainActivity. class );
// Call another new Activity
StartActivity (intent );
// Close the original Activity
TwoActivity. this. finish ();
}
};
}

Iii. Configuration File

AndroidMainfest. xml

1: when a new Activity is added to the system, it must be in AndroidMainfest. define a new Activity in xml, because we have created a new TwoActivity. java, so we need to go to AndroidMainfest. add the following code in xml. If no code is added, the system will cause a compilation error because Activity cannot be found.

<Activity android: name = ". TwoActivity"
Android: label = "@ string/app_name">
</Activity>

2: When two activities appear in the program, the system needs to decide which Activity the main program is. the following code is defined in xml. This parameter must be defined. If this parameter is not added, the program will not run.

<Category android: name = "android. intent. category. LAUNCHER"/> indicates that the program runs MainActivity rather than TwoActivity at startup.

<Activity android: name = ". MainActivity"
Android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>

Note:

1: The finish () method is called in the last line of the two java files. It indicates that the Activity has been completed. When the system receives the command, the Activity is closed, therefore, you click the return key on the simulator at this time and will not return to the screen of the previous Activity. To enable the return key on the simulator to return to the screen of the previous Activity, you can finish () comment out the method.

2: when the two activities are switching back and forth, they do not actually have two activities in switching, but re-call a new Activity when you click the button.

The running effect is as follows:

When we click the button in ActivityOne, the main control right of the Activity is handed over from ActivityOne to ActivityTwo. Then, when we click the button in ActivityTwo, the main control right of the Activity is handed over from ActivityTwo to ActivityOne.

  

  

 

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.