注意 onRestart的箭头是弄错了,应该指向onStart
MainActivity的布局文件和java代码
<?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:text="查看详情" > </Button> </LinearLayout>
package com.example.myactivityi; import androidx.appcompat.app.AppCompatActivity; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; /* * Activity四种状态: * 运行,暂停:可见 * 停止,销毁:不可见 * * 创建Activity的步骤: * 1.创建继承自Activity的Activity * 2.重写需要的回调方法 * 3.设置要显示的视图 * * 启动Activity: * 入口Activity:在AndroidManifest.xml中配置 * 其他Activity:调用startActivity(intent)方法 * 关闭Activity: * 在相应事件中调用finish(); * * */ public class MainActivity extends Activity { //重写活动运行前的三个方法 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.i("Activity生命周期","onCreate()方法调用"); //获取启动detail活动的按钮 Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { //创建intent对象 Intent intent = new Intent( MainActivity.this,DetailActivity.class ); //启动 startActivity(intent); } }); } @Override protected void onStart() { super.onStart(); Log.i("Activity生命周期","onStart()方法调用"); } //剩下三种状态的方法重写 @Override protected void onPause() { super.onPause(); Log.i("Activity生命周期","onPause()方法调用"); } @Override protected void onStop() { super.onStop(); Log.i("Activity生命周期","onStop()方法调用"); } @Override protected void onDestroy() { super.onDestroy(); Log.i("Activity生命周期","onDestroy()方法调用"); } }
detailActivity的布局文件和java代码
<?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/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="关闭" /> </LinearLayout>
package com.example.myactivityi; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import androidx.annotation.Nullable; /* 创建Activity的步骤: * 1.创建继承自Activity的Activity * 2.重写需要的回调方法 * 3.设置要显示的视图 * 4.配置,注册这个新的Activity */ //1.继承Activity public class DetailActivity extends Activity { //2.重写回调方法 @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); //3.设置要显示的视图 setContentView(R.layout.activity_detail); //退出 Button button = (Button)findViewById(R.id.button1); button.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { finish(); } }); } }