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
First create a test project project, choose the default layout, choose your favorite Theme (Theme).
Locate the XML file under layout in the File Manager and copy and paste it into the layout directory, named Second_activity.xml.
Next, create a new Java class at the. java file named Secondactivity.
You can now see an extra XML file and a Java class file in the file directory.
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>
Modify Secondactivity.xml:
<textview
Android:text= "The Second activity!"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>
To modify the Secondactivity.java file:
public class Secondactivity extends Actionbaractivity {
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.secondactivity);
}
}
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"/>
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"));
}
Set up an emulator to open the app with the simulator and start debugging.
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