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

 

 

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值