zoukankan      html  css  js  c++  java
  • Activity的生命周期

    前言:

    接触Android快两个礼拜了,虽然已开发了一个非常简单的app,但却还没有理清Activity的生命周期,自然对它里面的几种方法的含义没有彻底清楚的认识,今天好好总结一下。

    一、Activity的生命周期:

    (1)运行状态:Activity位于前台,用户可见,可以获得焦点。

    (2)暂停状态:其他Activity位于前台,该Activity依然可见,只是不能获得焦点。

    (3)停止状态:该Activity不可见,失去焦点。

    (4)销毁状态:该Activity结束,或者Activity所在进程结束。

    二、Activity的加载模式:

    好了,看到上面的图,相信你已经明白,activity什么时候加载哪个函数了,只需要在相应的函数中进行重写就可以了,下面程序进行简单测试,可以烧写进手机体验一下。

    Mainactivity:

    package com.example.x_yp.test;
    
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    public class MainActivity extends Activity
    {
        final String TAG = "--CrazyIt--";
        Button finish ,startActivity;
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            // 输出日志
            Log.d(TAG, "-------onCreate------");
            finish = (Button) findViewById(R.id.finish);
            startActivity = (Button) findViewById(R.id.startActivity);
            // 为startActivity按钮绑定事件监听器
            startActivity.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View source)
                {
                    Intent intent = new Intent(MainActivity.this
                            , SecondActivity.class);
                    startActivity(intent);
                }
            });
            // 为finish按钮绑定事件监听器
            finish.setOnClickListener(new OnClickListener()
            {
                @Override
                public void onClick(View source)
                {
                    // 结束该Activity
                    MainActivity.this.finish();
                }
            });
        }
        @Override
        public void onStart()
        {
            super.onStart();
            // 输出日志
            Log.d(TAG, "-------onStart------");
        }
        @Override
        public void onRestart()
        {
            super.onRestart();
            // 输出日志
            Log.d(TAG, "-------onRestart------");
        }
        @Override
        public void onResume()
        {
            super.onResume();
            // 输出日志
            Log.d(TAG, "-------onResume------");
        }
        @Override
        public void onPause()
        {
            super.onPause();
            // 输出日志
            Log.d(TAG, "-------onPause------");
        }
        @Override
        public void onStop()
        {
            super.onStop();
            // 输出日志
            Log.d(TAG, "-------onStop------");
        }
        @Override
        public void onDestroy()
        {
            super.onDestroy();
            // 输出日志
            Log.d(TAG, "-------onDestroy------");
        }
    }

    SecondActivity:

    package com.example.x_yp.test;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;
    
    public class SecondActivity extends Activity
    {
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            TextView tv = new TextView(this);
            tv.setText("SecondActivity");
            setContentView(tv);
        }
    }

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <Button
    android:id="@+id/startActivity"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="启动SecondActivity"
    />
    <Button
    android:id="@+id/finish"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="退出"
    />
    </LinearLayout>

    AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.x_yp.test" >
    
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity
                android:name=".SecondActivity"
                android:label="SecondActivity"
                android:theme="@style/AppTheme" >
            </activity>
        </application>
    
    </manifest>
  • 相关阅读:
    日期组件
    元素的隐藏和显示效果----利用定位
    盒模型
    数组的方法
    instanceof 和 typeof
    Tensorflow训练识别手写数字0-9
    Tensorflow问题集
    win7+vwmare12+centos7网络配制说明
    tesseract-OCR识别汉字及训练
    The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.
  • 原文地址:https://www.cnblogs.com/bewolf/p/4669803.html
Copyright © 2011-2022 走看看