Android tutorial (13) -- Activity Switching

Source: Internet
Author: User

In this chapter, we will learn how to switch between activities.

So many of the previous tutorials are operated in an Activity-View, so we may feel quite depressed.

Go straight to the topic. In Android software development, an application is usually composed of multiple activities and views. How can these activities be switched? Android provides the Intent class for inter-Activity communication.

Now let's take a look at how this Intent is used.

 


Step 1: Create a project and call it Ep. changeActivity. The remaining names remain unchanged. After the Activity is created, a new Activity is created and named Main2Activity. If it is correctly created, the Activity automatically adds activity_main2 to the view.

Let's talk about this step.

,

OK to complete an Activity-View.

Step 2: Write the View. To make it easier for students to understand, my View is relatively simple:

Activity_main.xml


[Java]
<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">
 
<TextView
Android: id = "@ + id/textView1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "111111"/>
 
<Button
Android: id = "@ + id/button1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_below = "@ + id/textView1"
Android: layout_centerHorizontal = "true"
Android: layout_marginTop = "63dp"
Android: text = "11111"/>
 
</RelativeLayout>

<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">

<TextView
Android: id = "@ + id/textView1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "111111"/>

<Button
Android: id = "@ + id/button1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_below = "@ + id/textView1"
Android: layout_centerHorizontal = "true"
Android: layout_marginTop = "63dp"
Android: text = "11111"/>

</RelativeLayout>
Activity_main2.xml


[Java]
<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 = ". Main2Activity">
 
<TextView
Android: id = "@ + id/textView1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "222222"/>
 
<Button
Android: id = "@ + id/button1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_below = "@ + id/textView1"
Android: layout_centerHorizontal = "true"
Android: layout_marginTop = "64dp"
Android: text = "22222"/>
 
</RelativeLayout>

<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 = ". Main2Activity">

<TextView
Android: id = "@ + id/textView1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "222222"/>

<Button
Android: id = "@ + id/button1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_below = "@ + id/textView1"
Android: layout_centerHorizontal = "true"
Android: layout_marginTop = "64dp"
Android: text = "22222"/>

</RelativeLayout>
It doesn't matter if you don't see it now. In the end, I will run it for you.

Step 3: CORE: Write activity:

MainActivity. java


[Java]
Package com. example. ep2.changeactivity;
 
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 bt;
 
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );

Bt = (Button) findViewById (R. id. button1 );

Bt. setOnClickListener (new OnClickListener (){

@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
Intent it = new Intent ();
It. setClass (MainActivity. this, Main2Activity. class );
StartActivity (it );
MainActivity. this. finish ();

}
});
}
 
}

Package com. example. ep2.changeactivity;

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 bt;

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );

Bt = (Button) findViewById (R. id. button1 );

Bt. setOnClickListener (new OnClickListener (){

@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
Intent it = new Intent ();
It. setClass (MainActivity. this, Main2Activity. class );
StartActivity (it );
MainActivity. this. finish ();

}
});
}

}

Main2Activity. java


[Java]
Package com. example. ep2.changeactivity;
 
Import android. OS. Bundle;
Import android. app. Activity;
Import android. content. Intent;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
 
Public class Main2Activity extends Activity {
 
Private Button bt;
 
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main2 );

Bt = (Button) findViewById (R. id. button1 );

Bt. setOnClickListener (new OnClickListener (){

@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
Intent it = new Intent ();
It. setClass (Main2Activity. this, MainActivity. class );
StartActivity (it );
Main2Activity. this. finish ();
}
});
}
 
}

Package com. example. ep2.changeactivity;

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

Public class Main2Activity extends Activity {

Private Button bt;

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main2 );

Bt = (Button) findViewById (R. id. button1 );

Bt. setOnClickListener (new OnClickListener (){

@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
Intent it = new Intent ();
It. setClass (Main2Activity. this, MainActivity. class );
StartActivity (it );
Main2Activity. this. finish ();
}
});
}

}

 

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.