活动Activity——Activity的生命周期&各状态之间的切换过程——重点
下面是Activity与生命周期有关的方法说明。
onCreate:创建活动。把页面布局加载进内存,进入了初始状态。
onStart:开始活动。把活动页面显示在屏幕上,进入了就绪状态。
onResume:恢复活动。活动页面进入活跃状态,能够与用户正常交互,例如允许响应用户的点击动作、允许用户输入文字等等。
onPause:暂停活动。页面进入暂停状态,无法与用户正常交互。
onStop:停止活动。页面将不在屏幕上显示。
onDestroy:销毁活动。回收活动占用的系统资源,把页面从内存中清除。
onRestart:重启活动。重新加载内存中的页面数据。
onNewIntent:重用已有的活动实例。
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
=================================================================================
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/btn_act_next" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="跳到下个页面" android:textColor="#000000" android:textSize="17sp" /> <TextView android:id="@+id/tv_life" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:textColor="#000000" android:textSize="17sp" /> </LinearLayout>
ainActivity的代码:
package com.example.myapplication; 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.TextView; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private final static String TAG = "ActLifeActivity"; private TextView tv_life; // 声明一个文本视图对象 private String mStr = ""; private void refreshLife(String desc) { // 刷新生命周期的日志信息 Log.d(TAG, desc); mStr = String.format("%s%s %s %s\n", mStr, DateUtil.getNowTimeDetail(), TAG, desc); tv_life.setText(mStr); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.btn_act_next).setOnClickListener(this); tv_life = findViewById(R.id.tv_life); // 从布局文件中获取名叫tv_life的文本视图 refreshLife("onCreate"); // 刷新生命周期的日志信息 } @Override protected void onStart() { // 开始活动 super.onStart(); refreshLife("onStart"); // 刷新生命周期的日志信息 } @Override protected void onStop() { // 停止活动 super.onStop(); refreshLife("onStop"); // 刷新生命周期的日志信息 } @Override protected void onResume() { // 恢复活动 super.onResume(); refreshLife("onResume"); // 刷新生命周期的日志信息 } @Override protected void onPause() { // 暂停活动 super.onPause(); refreshLife("onPause"); // 刷新生命周期的日志信息 } @Override protected void onRestart() { // 重启活动 super.onRestart(); refreshLife("onRestart"); // 刷新生命周期的日志信息 } @Override protected void onDestroy() { // 销毁活动 super.onDestroy(); refreshLife("onDestroy"); // 刷新生命周期的日志信息 } @Override protected void onNewIntent(Intent intent) { // 重用已有的活动实例 super.onNewIntent(intent); refreshLife("onNewIntent"); // 刷新生命周期的日志信息 } @Override public void onClick(View v) { if (v.getId() == R.id.btn_act_next) { // 从当前页面跳到指定的活动页面 startActivity(new Intent(this, ActNextActivity.class)); } } }
第二个页面:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/btn_act_pre" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="返回上个页面" android:textColor="#000000" android:textSize="17sp" /> <TextView android:id="@+id/tv_life" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:textColor="#000000" android:textSize="17sp" /> </LinearLayout>
package com.example.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class ActNextActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_act_next);
}
}
DateUtil
package com.example.myapplication; import android.annotation.SuppressLint; import java.text.SimpleDateFormat; import java.util.Date; @SuppressLint("SimpleDateFormat") public class DateUtil { // 获取当前的日期时间 public static String getNowDateTime() { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); return sdf.format(new Date()); } // 获取当前的时间 public static String getNowTime() { SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); return sdf.format(new Date()); } // 获取当前的时间(精确到毫秒) public static String getNowTimeDetail() { SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS"); return sdf.format(new Date()); } }
==========================================================================================
第一步:打开app
按home键,然后再打开app
==============================================================================
打开app:
按切进程键,然后再打开app:
====================================================================
打开app,然后退出:
===============================================================
打开app,跳转到下个页面
========================================================================
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!