• 活动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,跳转到下个页面

     

     

    ========================================================================

  • 相关阅读:
    微信公众号开发第一版
    关于AJAX
    Node——异步I/O机制
    boostrap框架学习
    less学习笔记
    this指向
    关于js作用域
    mybatis映射mapper文件的#{}和${}的区别和理解
    Eclipse国内镜像源配置
    eclipse优化加速提速,解决eclipse卡、慢的问题
  • 原文地址:https://www.cnblogs.com/xiaobaibailongma/p/16439938.html
Copyright © 2020-2023  润新知