安卓——Activity的生命周期





 

运行程序先经过前三个步骤:打印在Logcat里,如下图



退出程序,点击手机下方的后退按钮,就会执行后三个步骤






先打印前三个步骤,然后按手机的home键,打印onPause方法和onStop方法,这样程序就到后台了。再打开程序打印上图中最后三个步骤


上面两个不同的Activity周期基于下面这个代码查看的

package com.example.lenovo.tablelayout;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {
    final String TAG = "tag";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG,"MainActivity-->onCreate");
    }
    @Override
   protected void onStart()
    {
        super.onStart();
        Log.i(TAG,"MainActivity-->onStart");
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        Log.i(TAG,"MainActivity-->onResume");
    }
    @Override
    protected void onPause()
    {
        super.onPause();
        Log.i(TAG,"MainActivity-->onPause");
    }
    @Override
    protected void onStop()
    {
        super.onStop();
        Log.i(TAG,"MainActivity-->onStop");
    }
    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        Log.i(TAG,"MainActivity-->onDestroy");
    }
}




启动第一个Activity,,先执行前面三个流程。

然后我点击屏幕中的clear按钮,就会出现下图。


又出现一个Activity,而且他的风格是透明的,可以看见第一个Activity。然后又打印了一个onPause方法,表明第一个Activity失去了焦点。


然后点击手机下方的后退按钮第一个Activity又出现了,说明它又得到了焦点。此时屏幕下方又打印了一个onResume方法。


这流程的所有代码如下

第一个Activity

package com.example.lenovo.tablelayout;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
    final String TAG = "tag";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG,"MainActivity-->onCreate");
        //用第一个Activity中的xml(activity_main.xml)文件中的id为clear的按钮,添加一个点击事件,实现Activity的变化
        Button btn=(Button) findViewById(R.id.clear);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,Secend_Activity.class);

                MainActivity.this.startActivity(intent);
            }
        });
    }
    @Override
   protected void onStart()
    {
        super.onStart();
        Log.i(TAG,"MainActivity-->onStart");
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        Log.i(TAG,"MainActivity-->onResume");
    }
    @Override
    protected void onPause()
    {
        super.onPause();
        Log.i(TAG,"MainActivity-->onPause");
    }
    @Override
    protected void onStop()
    {
        super.onStop();
        Log.i(TAG,"MainActivity-->onStop");
    }
    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        Log.i(TAG,"MainActivity-->onDestroy");
    }
}

第二个Activity

package com.example.lenovo.tablelayout;

import android.app.Activity;
import android.os.Bundle;

/**
 * Created by lenovo on 2018/2/26.
 *
 * 第二Activity类,继承了Activity,加载了一个xml文件(calculator。xml)
 */

public class Secend_Activity extends Activity {
    @Override
    protected  void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);
        setContentView(R.layout.calculator);
    }
}

第一个Activity的xml文件(activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stretchColumns="*">
    <TextView
        android:background="#f0f0f0"
        android:id="@+id/textView1"
        android:layout_height="40dp"
        android:layout_width="wrap_content"
        android:text="90"
        />



    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="7" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="8" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="9" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="/" />
   </TableRow>
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="4" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="5" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="6" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="*" />
    </TableRow>
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="3" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="-" />

    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="." />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="=" />

    </TableRow>

    <Button
        android:id="@+id/clear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="clear" />

</TableLayout>

第二个Activity的xml文件(caculator.xml)

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:background="#f0f0f0"
            android:id="@+id/textView1"
            android:layout_height="40dp"
        android:layout_width="wrap_content"
        android:text="90"
         />

    <TableRow
        android:id="@+id/rw1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        >
    <Button
        android:text="7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
        <Button
            android:text="7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <Button
            android:text="7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <Button
            android:text="7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />

    </TableRow>

</TableLayout>

要想让第二Activity能够出现,需要注册一下。

在AndroidMainfiles.xml文件中添加这样两句话

<activity
    android:name="com.example.lenovo.tablelayout.Secend_Activity"

    android:theme="@android:style/Theme.DeviceDefault.Dialog"
    />
下面是添加完后整个AndroidMainfiles.xml文件的内容

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lenovo.tablelayout">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.lenovo.tablelayout.Secend_Activity"

            android:theme="@android:style/Theme.DeviceDefault.Dialog"
            />
    </application>

</manifest>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水之积也不厚,则其负大舟也无力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值