Implement a jump between Android activity

Source: Internet
Author: User

Android programs generally do not have only one activity, and will encounter jumps between the activity. The following is a jump using intent to do the activity inside the application. For example, the first activity of an application is:

After clicking the "Next" button:

This requires writing two activity classes. The first one is: mainactivity

1  PackageCom.easymorse;2 3 Importandroid.app.Activity;4 Importandroid.content.Intent;5 ImportAndroid.os.Bundle;6 ImportAndroid.view.View;7 ImportAndroid.view.View.OnClickListener;8 ImportAndroid.widget.Button;9 Ten  Public classMainactivityextendsActivity { One  A     Privatebutton button; -  -     /**Called when the activity is first created.*/ the @Override -      Public voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.main); +  -          This. Button = (Button) This. Findviewbyid (R.id.button01); +          This. Button.setonclicklistener (NewOnclicklistener () { A @Override at              Public voidOnClick (View v) { -                 Intent Intent = new Intent (); -                 Intent.setclass (mainactivity.this, Nextactivity.class); -  startactivity (Intent); -             } -         }); in     } -}

The second one is: nextactivity

1  PackageCom.easymorse;2 3 Importandroid.app.Activity;4 ImportAndroid.os.Bundle;5 6  Public classNextactivityextendsActivity {7 @Override8     protected voidonCreate (Bundle savedinstancestate) {9         Super. OnCreate (savedinstancestate);Ten          This. Setcontentview (r.layout.next_activity); One     } A}

Then, add the two activity declarations to the androidmanifest.xml.

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Manifestxmlns:android= "Http://schemas.android.com/apk/res/android"3  Package= "Com.easymorse"4 Android:versioncode= "1"5 Android:versionname= "1.0">6     <ApplicationAndroid:icon= "@drawable/icon"Android:label= "@string/app_name">7         <ActivityAndroid:name=". Mainactivity "8 Android:label= "@string/app_name">9             <Intent-filter>Ten                 <ActionAndroid:name= "Android.intent.action.MAIN" /> One                 <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> A             </Intent-filter> -         </Activity> -  the     <ActivityAndroid:name= "Nextactivity"></Activity> - </Application> -     <USES-SDKandroid:minsdkversion= "3" /> -  + </Manifest> 

Increase the constant string in String.xml:

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Resources>3     <stringname= "Hello">Hello World, mainactivity!</string>4     <stringname= "App_name">Activity.forward.demo</string>5     <stringname= "Next_button">Next</string>6 </Resources>

In addition to the original main.xml in the layout directory:

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 android:orientation= "vertical"4 Android:layout_width= "Fill_parent"5 Android:layout_height= "Fill_parent"6     >7 <TextView8     Android:layout_width= "Fill_parent" 9 Android:layout_height= "Wrap_content" Ten Android:text= "@string/hello" One     /> A <ButtonAndroid:text= "@string/next_button"Android:id= "@+id/button01"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"></Button> - </LinearLayout>

You also need to create a nextactivity layout file declaration, such as Next_activity.xml:

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayout3   xmlns:android= "Http://schemas.android.com/apk/res/android"4 Android:layout_width= "Wrap_content"5 Android:layout_height= "Wrap_content">6 <TextViewAndroid:text= "@string/next_button"Android:id= "@+id/textview01"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"></TextView>7 </LinearLayout>

In this case, if you press the fallback key back to mainactivity. If you do not want to fall back to the previous activity, quit. Need this:

1          This. Button = (Button) This. Findviewbyid (R.id.button01);2          This. Button.setonclicklistener (NewOnclicklistener () {3 @Override4              Public voidOnClick (View v) {5Intent Intent =NewIntent ();6Intent.setclass (mainactivity. This, Nextactivity.class);7 startactivity (intent);8 finish ();9             }Ten});

Implement a jump between activity. rar

Implement a jump between Android activity

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.