欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android学习笔记(Android Studio) 4-1-2 Activity的生命周期(不可不会的Activity和Fragment)

程序员文章站 2023-12-31 14:49:04
...

Android学习笔记4-1-2


推荐新手向学习视频:B站https://www.bilibili.com/video/av38409964点我传送


4-1-1 Activity的生命周期

Android学习笔记(Android Studio) 4-1-2 Activity的生命周期(不可不会的Activity和Fragment)

  • LifeCycleActivity.java

      package com.ylw.helloworld;
      
      import android.os.Bundle;
      import android.util.Log;
      
      import androidx.annotation.Nullable;
      import androidx.appcompat.app.AppCompatActivity;
      
      public class LifeCycleActivity extends AppCompatActivity {
          @Override
          protected void onCreate(@Nullable Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_test);
              Log.d("LifeCycle","----onCreate----");
          }
      
          @Override
          protected void onStart() {
              super.onStart();
              Log.d("LifeCycle","----onStart----");
          }
      
          @Override
          protected void onResume() {
              //可以用于数据的刷新
              super.onResume();
              Log.d("LifeCycle","----onResume----");
          }
      
          @Override
          protected void onPause() {
              //当你离开当前应用去别的应用时会调用此方法暂停
              super.onPause();
              Log.d("LifeCycle","----onPause----");
          }
      
          //当程序被切入后台时会调用onPause和onStop
      
          @Override
          protected void onStop() {
              super.onStop();
              Log.d("LifeCycle","----onStop----");
          }
      
          @Override
          protected void onDestroy() {
              super.onDestroy();
              Log.d("LifeCycle","----onDestroy----");
          }
      
          @Override
          protected void onRestart() {
              super.onRestart();
              Log.d("LifeCycle","----onRestart----");
          }
      }
    

上一篇:

下一篇: