Android_ define multiple activity and jumps

Source: Internet
Author: User

Description : Create multiple activity in the Android app, and start an activity method, as well as jump between activity.

Example : Add a button to the mainactivity, touch the button, jump to secondactivity.

Step : 1, define a class, inherit activity. The OnCreate method of the replication activity.

Package Com.away.b_01_multiactivity;import Android.app.activity;import Android.os.bundle;public class Secondactivity extends activity{@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.second);}}
At the same time, a new layout file Second.xml is created, and Setcontentview is called in the OnCreate method of secondactivity to set the layout file used by secondactivity.
<textview        android:id= "@+id/secondtextview"        android:layout_width= "match_parent"        android:layout_ height= "Wrap_content"        android:text= "second activity"/>
2, in the Androidmainfest.xml file whichBrochure Activity。

<activity      android:name= "com.away.b_01_multiactivity. 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><activity      android:name= "com.away.b_01_multiactivity. Secondactivity "      android:label=" second "></activity>
Ps:<intent-filter> as the child tag of the activity. It does this by setting up an activity that the activity starts by default for the application. Android:label's role is that the activity shows the name on the phone.

Here the activity is created, and the following implementation jumps between the activity.

3, add a <Button> in Activity_main.xml.

<button         android:id= "@+id/button"        android:layout_width= "match_parent"        android:layout_height= " Wrap_content "        android:layout_below=" @id/textview "        android:text=" Start secondativity "/>
4. Create the object of intent in Mainactivity.java (Intent), call the SetClass method to set the activity to start. Call the StartActivity method to start the activity.
Package Com.away.b_01_multiactivity;import Android.app.activity;import Android.content.intent;import Android.os.bundle;import Android.view.menu;import Android.view.view;import Android.view.View.OnClickListener;    Import Android.widget.button;public class Mainactivity extends Activity {private button button;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                Setcontentview (R.layout.activity_main);        button= (Button) Findviewbyid (R.id.button);    Button.setonclicklistener (New Buttonlistener ()); } class Buttonlistener implements onclicklistener{@Overridepublic void OnClick (View v) {Intent intent=new Intent ();//se The first parameter of the Tclass function is a context object//context is a class, activity is a subclass of the context class, that is, all activity objects can be transformed upward into a context object// The second parameter of the SetClass function is the class object, in the current scenario, the Class object Intent.setclass (Mainactivity.this, which needs to be activated for activity) should be passed in.    Secondactivity.class); startactivity (intent);} }}

Can also be written like this:

Intent Intent = new Intent (mainactivity.this, Secondactivity.class); startactivity (Intent);

It can also be written like this:

StartActivity (New Intent (Mainactivity.this, Secondactivity.class));

Just the two sentences. Can be achieved from the Mainactivity page to jump to the Secoidactivity page.

Attached: (Original not easy, reproduced please indicate the Source =-=)


Structure diagram:


Sometimes, in the jump page also need to pass data, this time can see here >>>activity pass value.

Welcome to AC http://blog.csdn.net/ycwol/article/details/39736075



Android_ define multiple activity and jumps

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.