Android Studio Tutorial: [6] Create multiple activity

Source: Internet
Author: User

Generally speaking, an Android application has more than one activity, more than one interface. So we need to create multiple activities to meet the requirements of the application, here I will show you how to add new activity, and to achieve the switch between activity, where two events are displayed in different interfaces, the switch of the activity is equivalent to the interface switch.

Tools/Materials
    • Android Studio
    • Android Phone or Android simulator
Method/Step
  1. 1

    First create a test project project, choose the default layout, choose your favorite Theme (Theme).

  2. Locate the XML file under layout in the File Manager and copy and paste it into the layout directory, named Second_activity.xml.

  3. Next, create a new Java class at the. java file named Secondactivity.

  4. You can now see an extra XML file and a Java class file in the file directory.

  5. Open the Androidmanifest.xml file, add a <activity></activity> tag, and add the following:

    <activity

    Android:name= ". Secondactivity "

    Android:label= "Second Activity" >

    <intent-filter>

    <action android:name= "Com.litreily.SecondActivity"/>

    <category android:name= "Android.intent.category.DEFAULT"/>

    </intent-filter>

    </activity>

  6. Modify Secondactivity.xml:

    <textview

    Android:text= "The Second activity!"

    Android:layout_width= "Wrap_content"

    android:layout_height= "Wrap_content"/>

  7. To modify the Secondactivity.java file:

    public class Secondactivity extends Actionbaractivity {

    @Override

    protected void OnCreate (Bundle savedinstancestate) {

    Super.oncreate (savedinstancestate);

    Setcontentview (r.layout.secondactivity);

    }

    }

  8. Modify the main interface's Activity_my.xml file and add a button to jump to activity:

    <button

    Android:layout_width= "Fill_parent"

    android:layout_height= "Wrap_content"

    Android:text= "Go to Next Interface"

    android:onclick= "OnClick"/>

  9. The button's Click event Response function onclick is implemented in the Java class of the main interface, and the new activity is opened through the StartActivity function.

    public void OnClick (view view)

    {

    StartActivity (New Intent ("com.litreily.SecondActivity"));

    }

  10. Set up an emulator to open the app with the simulator and start debugging.

  11. Run the program in the simulator, click the button can switch activity, in the second interface click the Back button to return to the main interface.

Android Studio Tutorial: [6] Create multiple activity

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.