Define multiple activities and jumps.
Note: multiple activities are created in the Android application, and the method for starting an activity and redirection between the activities is also provided.
Example: Add a button in MainActivity, touch the button, and jump to SecondActivity.
Step: 1. Define a class that inherits Activity and rewrites the OnCreate method in 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);}}Create a layout file second. xml and call setContentView 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. register the Activity in the AndroidMainfest. xml file.
<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> is the sub-tag of the activity. It sets an activity as the activity started by the application by default. Android: label indicates the name of the activity displayed on the mobile phone.
Here the activity is created, but how can we achieve inter-activity jump?
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. Generate the Intent object (Intent) in MainActivity. java, call the setClass method to set the Activity to be started, and 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 (); // The first parameter of the setClass function is a Context object // Context is a class, and Activity is a subclass of the Context class, that is, all Activity objects can be transformed up to the Context object // The second parameter of the setClass function is the Class object. In the current scenario, the class Object intent of the Activity to be started should be input. setClass (MainActivity. this, SecondActivity. class); startActivity (intent );}}}Attached: (originality is not easy. for reprinting, please indicate the source =-=)
Structure:
An error occurred while redirecting to multiple activities.
Check whether the main configuration file has registered these activities. If yes, check the layout file for errors. Please adopt it. Thank you! If it still doesn't work, I can help you debug it at night!
Example of how Android redirects between multiple activities
Code: FromXXX => GotoXXX
...
Intent intentTo = new Intent ();
IntentTo. setClass (FromXXX. this, GotoXXX. class );
StartActivity (intentTo );