Android开发-安卓生命周期

 

5.1、回调函数

Activity定义了7个回调函数来对应生命周期的不同状态的改变

onCreate():在活动创建时调用stop

onStart():在活动为用户可见时调用

onResume():在应用程序与用户开始交互时被调用

onPause():活动暂停无法与用户交互时调用

onStop():活动不可见时调用

onDestory():活动被系统销毁之前调用

onRestart():当活动在destory前stop后重新打开时调用

 
public class AMain extends Activity {
     @Override
     protected void onCreate(@Nullable Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.ly_view);
         Log.i("mytag","oncreate");
     }
 ​
     @Override
     protected void onStart() {
         super.onStart();
         Log.i("mytag","onstrat");
     }
 ​
     @Override
     protected void onResume() {
         super.onResume();
         Log.i("mytag","onresume");
     }
 ​
     @Override
     protected void onPause() {
         super.onPause();
         Log.i("mytag","onpause");
     }
 ​
     @Override
     protected void onStop() {
         super.onStop();
         Log.i("mytag","onstop");
     }
 ​
     @Override
     protected void onDestroy() {
         super.onDestroy();
         Log.i("mytag","ondestory");
     }
     @Override
     protected void onRestart() {
         super.onRestart();
         Log.i("mytag","onrestart");
     }
 }

5.2、生命周期的执行(cycle)

 

用户打开用户到交互时,执行的三个回调函数

当用户关闭程序时

 

调用了结束程序的三个回调函数

当用户打开程序,但没有销毁程序,即程序进入后台执行时

系统不会调用destory函数

当用户从后台再次打开应用时,就会调用restart函数

5.3、界面切换的生命周期

新建副页面

布局ly_view.xml:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical"
     >
 <!--当layout布局中出现两个以上的控件时,需要手动设置排列方式-->
     <TextView android:text="Main"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"/>
     <!--由于需要在java中获取对象,设置id-->
     <Button android:id="@+id/btn_gogo" android:text="gogo"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"/>
 </LinearLayout>

副页面布局:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent">
 ​
     <TextView android:text="act1"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"/>
 </LinearLayout>

清单注册

 <activity android:name=".AMain" android:exported="true">
    <!--主界面-->
     <intent-filter>
         <action android:name="android.intent.action.MAIN"/>
         <category android:name="android.intent.category.LAUNCHER"/>
     </intent-filter>
 </activity>
 <activity android:name=".Act1" android:exported="true"/>

5.3、Log日志打印

    public static final int ASSERT = 7;
     public static final int DEBUG = 3;//Log.d
     public static final int ERROR = 6;//Log.e
     public static final int INFO = 4;//Log.i
     public static final int VERBOSE = 2;//Log.v
     public static final int WARN = 5;//Log.w

格式:Log.i("mytag","message");

mytag:自定义标签名,可用于筛选

message:定义想要显示的信息

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值