一个项目里面可以有多个Activity
AndroidManifest.xml
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
在谁的里面就运行谁
在两个Activity 里切换 package com.example.chapter31; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.i("Activity1","onCreate"); Button button =(Button)findViewById(R.id.button); button.setOnClickListener (new View.OnClickListener(){ @Override public void onClick (View view){ Intent intent = new Intent (MainActivity.this,SecondActivity.class); startActivity(intent); }}); } @Override protected void onStart() { super.onStart(); Log.i("Activity1","onStart()"); } @Override protected void onRestart() { super.onRestart(); Log.i("Activity1","onRestart()"); } @Override protected void onResume() { super.onResume(); Log.i("Activity1","onResume()"); } @Override protected void onPause() { super.onPause(); Log.i("Activity1","onPause()"); } @Override protected void onStop() { super.onStop(); Log.i("Activity1","onStop()"); } @Override protected void onDestroy() { super.onDestroy(); Log.i("Activity1","onDestroy()"); } } <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id = "@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight = "1" android:text = "跳转到第二个Activity" /> </LinearLayout> package com.example.chapter31; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; public class SecondActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout1); Log.i("Activity1","onCreate"); Button button = (Button)findViewById(R.id.button); button.setOnClickListener (new View.OnClickListener(){ @Override public void onClick (View view){ Intent intent = new Intent (SecondActivity.this,MainActivity.class); 从2到1 startActivity(intent); }}); } @Override protected void onStart() { super.onStart(); Log.i("Activity2","onStart()"); } @Override protected void onRestart() { super.onRestart(); Log.i("Activity2","onRestart()"); } @Override protected void onResume() { super.onResume(); Log.i("Activity2","onResume()"); } @Override protected void onPause() { super.onPause(); Log.i("Activity2","onPause()"); } @Override protected void onStop() { super.onStop(); Log.i("Activity2","onStop()"); } @Override protected void onDestroy() { super.onDestroy(); Log.i("Activity2","onDestroy()"); } } <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".SecondActivity"> <Button android:id = "@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight = "1" android:text = "跳转到Activity1" /> </LinearLayout> 别忘了在AndroidManifest 里面注册