Activity生命周期.lanchMode.保存状态

Activity生命周期

  每一个Android应用程序在运行时,对于底层的Linux Kernel而言都是一个单独的进程,但是对于Android系统而言,因为局限于手机画面的大小与使用的考虑,不能把每一个运行中的应用程序窗口都显示出来。

  所以通常手机系统的界面一次仅显示一个应用程序窗口,Android使用了Activity的概念来表示界面。

  运行中的应用程序分为五大类,分别是:

    前景模式:foreground process

    可见模式:visible process

    背景模式:background process

    空白模式:empty process

    服务模式:service process

  除了最后一个,貌似service process是Service的事情了。其他都与Activity相关。

  Android系统会判断应用程序Activity是属于哪一个类,给予不同的Activity生命周期。      

  Activity的生命周期也是它所在进程的生命周期。

  Activity生命周期的运行如图

  

 

                       

Activity生命周期进程类型

  在Android系统中,即使不关掉程序,当应用程序处于某种进程类时,也有可能被系统kill

  Android系统通过运行机制,依照哪些画面或消息对使用者最重要以及当前内存使用状况,而做出是否kill Activity的决定。

  foreground process(前景模式)是当前显示于手机屏幕上的应用程序画面,被定义为前景模式的进程,其中由onCreate()、onStart() 、onResume() 函数调用的Activity都会变成foreground process正在运行的Activity。

  visible process(可见模式):visible process最常发生的情况是当应用程序弹出对话框要与用户交互时,原应用程序就会变成透明(不可见)的,而对话窗口就会变成前景。

  当对话窗口退出后,原应用程序马上就又变回原前景模式了。

  在Activity窗口画面变为透明时,就会由onPause()函数掌控进入暂停状态。

  当前景进程退出时,该Activity就会再度被拉到前景模式,由onResume() 函数唤醒。

  background process是在Activity窗口画面被其他Activity完全盖掉,窗口画面已经完全看不见时,则会进入onStop()停止状态。

  这种情况通常发生在两个不同的应用程序开启时,后开启的应用程序会覆盖掉原应用程序。

  此时对background process Activity的处理有两种选择:一是直接被onDestroy()退出,该程序将完全关闭,无法再使用任何返回键回到该程序;另一个处理方式是当其他Activity需要内存时,这个background process会先被清除掉,释放出内存。

  如果使用者再度浏览刚刚被清除掉的background process,则Android系统会自动再运行onCreate()重新启动该Activity,所以当系统需要内存时,就会暂时将背景进程清除,让它变成empty process(空白模式) , 所以空白进程最重要的目的就是暂时释放出内存,直到使用者再度唤醒该empty process Activity时,才会将空白进程变成前景进程。

  (Service相关)service process(服务模式进程)是由startService()所产生的,虽然服务进程有点类似背景进程在背景状态运行,但是它的运行等级和前景进程几乎一样高。

  服务模式进程是持续运行的,虽然使用者看不到任何运行画面,Android系统不会自动关闭此类的服务进程,除非使用者自行关闭。这部分内容可在Service详解里面再讨论。

 

金字塔型的生命周期

 

 

  Activity生命周期的每一个阶段都表示为金字塔上的一个台阶,当系统创建一个新的activity时,每一个回调函数都把activity的状态网上挪一步。

  金子塔的最顶层就是activity运行在前景模式下,用户可与之交互。

  当用户离开activity时,系统调用另一些回调函数,将activity的状态从金字塔中一步一步移下来。有些情况下,activity只移动一部分,并没有完全到底,这些情况下仍然可以移动回顶部。

  注意这些状态中只有三个状态是静态(static)的,意味着activity只有在这三个状态下能停留一段时间:

    Resumed:foreground,用户可交互running state

    Paused:部分被遮挡,不能接收用户输入也不能执行代码,另一个半透明或者小的activity正挡在前面。

    Stopped:activity完全被遮挡,不能被用户看到,activity被认为在background,当Stopped的时候,activity实例的状态信息被保留,但是不能执行任何代码。

  其他状态都是转换状态,系统会很快调用其他相应的回调函数离开这些状态。比如系统调用onCreate()之后,会很快调用onStart(),之后是 onResume()。

 

回调函数

  覆写这些回调函数时,首先要记得一定要调用基类的回调函数,即最开始一行永远是super.onXXX();

  onPause()和onResume()中的动作应该互逆,比如说onPause()中释放了相机,那么onResume()中就要重新初始化相机。

 

  Stopped状态下,UI对用户完全不可见,此时用户焦点在另一个activity或者另一个程序上。

  onStop()中需要释放资源,因为有时候系统会kill掉Stopped状态的进程,如果有资源没有被释放,会造成内存泄露。

  onStop()中还应该包括一些关闭操作,比如向数据库写信息。

  当从Stopped状态回到前景时,首先需要调用onRestart(),这个函数做一些恢复工作,恢复停止但是并没有被销毁的activity;然后系统会接着调用onStart(),因为每次activity变为可见时都要调用onStart()。

  可以把onStart()和onStop()看成一对,因为在一开始启动时和重新启动时都需要做一些初始化工作。

 

  onDestroy()一般都是在onPause()和onStop()之后调用,但有一个例外的情况:如果在onCreate()中调用finish()方法,系统将会立即调用onDestroy()而不用经过生命周期中的其他阶段。

 

重新创建Activity

  如果activity是自己销毁的,实例就永远消失了,但是如果系统因为资源限制销毁了activity,虽然这个实例已经不在了,但是当用户返回到它时,系统会利用这个activity被销毁时存储的数据,重新创建一个实例。(这个有点说来话长,且听下回分解吧。)

  重建Activity的详细解释:http://www.cnblogs.com/mengdd/archive/2012/12/17/2822291.html

 

Activity生命周期测试程序

  注释部分来自于《Professional Android 4 Application Development》: 

复制代码
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class LifeOfActivity extends Activity
{

    private static final String TAG = "ActivityLife";

    // Called at the start of the full lifetime.
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_life_of);
        Log.e(TAG, "start onCreate~~~");

        // Initialize Activity and inflate the UI.
    }

    // Called after onCreate has finished, use to restore UI state.
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState)
    {
        super.onRestoreInstanceState(savedInstanceState);
        Log.e(TAG, "start onRestoreInstanceState~~~");

        // Restore UI state from the savedInstanceState.
        // This Bundle has also been passed to onCreate.
        // Will only be called if the Activity has been killed by the system
        // since it was last visible.
    }

    // Called at the start of the visible lifetime.
    @Override
    protected void onStart()
    {
        super.onStart();
        Log.e(TAG, "start onStart~~~");
        // Apply any required UI change now that the Activity is visible.
    }

    // Called before subsequent visible lifetimes for an Activity process.
    @Override
    protected void onRestart()
    {
        super.onRestart();
        Log.e(TAG, "start onRestart~~~");

        // Load changes knowing that the Activity has already been visible
        // within this process.
    }

    // Called at the start of the active lifetime
    @Override
    protected void onResume()
    {
        super.onResume();
        Log.e(TAG, "start onResume~~~");
        // Resume any paused UI updates, threads, or processes required
        // by the Activity but suspended when it was inactive.

    }

    // Called to save UI state changes at the end of the active lifecycle.
    @Override
    protected void onSaveInstanceState(Bundle outState)
    {
        super.onSaveInstanceState(outState);
        Log.e(TAG, "start onSaveInstanceState~~~");

        // Save UI state changes to the outState(savedInstanceState).
        // This Bundle will be passed to onCreate and onRestoreInstanceState if
        // the process is killed and restarted by the run time.
    }

    // Called at the end of the active lifetime.
    @Override
    protected void onPause()
    {
        super.onPause();
        Log.e(TAG, "start onPause~~~");
        // Suspend UI updates, threads, or CPU intensive processes that don't
        // need to be updated when the Activity isn't the active foreground
        // Activity.
    }

    // Called at the end of the visible lifetime.
    @Override
    protected void onStop()
    {
        super.onStop();
        Log.e(TAG, "start onStop~~~");

        // Suspend remaining UI updates, threads, or processing that aren't
        // required when the Activity isn't visible.
        // Persist all edits or state changes as after this call the process if
        // likely to be killed.
    }

    // Sometimes called at the end of the full lifetime.
    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        Log.e(TAG, "start onDestroy~~~");
        // Clean up any resources including ending threads, closing databases
        // connections etc.
    }

}
复制代码

 

参考资料

官网参考链接:

http://developer.android.com/reference/android/app/Activity.html

http://developer.android.com/training/basics/activity-lifecycle/starting.html

http://developer.android.com/training/basics/activity-lifecycle/pausing.html

http://developer.android.com/training/basics/activity-lifecycle/stopping.html

http://developer.android.com/training/basics/activity-lifecycle/recreating.html

http://developer.android.com/guide/components/activities.html

 

两分钟让你明白Acitivity生命周期:

http://www.apkbus.com/blog-99192-39550.html

四大组件:

http://www.apkbus.com/android-18204-1-1.html

 

 

 

Activity生命周期

http://blog.csdn.net/liuhe688/article/details/6733407

四种lanchmode:

http://blog.csdn.net/liuhe688/article/details/6754323

 

onSaveInstanceState方法会在什么时候被执行,有这么几种情况:


1、当用户按下HOME键时。

这是显而易见的,系统不知道你按下HOME后要运行多少其他的程序,自然也不知道activity A是否会被销毁,故系统会调用onSaveInstanceState,让用户有机会保存某些非永久性的数据。以下几种情况的分析都遵循该原则


2、长按HOME键,选择运行其他的程序时。


3、按下电源按键(关闭屏幕显示)时。


4、从activity A中启动一个新的activity时。


5、屏幕方向切换时,例如从竖屏切换到横屏时。

在屏幕切换之前,系统会销毁activity A,在屏幕切换之后系统又会自动地创建activity A,所以onSaveInstanceState一定会被执行

 

总而言之,onSaveInstanceState的调用遵循一个重要原则,即当系统“未经你许可”时销毁了你的activity,则onSaveInstanceState会被系统调用,这是系统的责任,因为它必须要提供一个机会让你保存你的数据(当然你不保存那就随便你了)。

 


至于onRestoreInstanceState方法,需要注意的是,onSaveInstanceState方法和onRestoreInstanceState方法“不一定”是成对的被调用的,onRestoreInstanceState被调用的前提是,activity A“确实”被系统销毁了,而如果仅仅是停留在有这种可能性的情况下,则该方法不会被调用,例如,当正在显示activity A的时候,用户按下HOME键回到主界面,然后用户紧接着又返回到activity A,这种情况下activity A一般不会因为内存的原因被系统销毁,故activity A的onRestoreInstanceState方法不会被执行


另外,onRestoreInstanceState的bundle参数也会传递到onCreate方法中,你也可以选择在onCreate方法中做数据还原.

 

 

 

 

复制代码
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class LifeOfActivity extends Activity
{

    private static final String TAG = "ActivityLife";

    // Called at the start of the full lifetime.
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_life_of);
        Log.e(TAG, "start onCreate~~~");

        // Initialize Activity and inflate the UI.
    }

    // Called after onCreate has finished, use to restore UI state.
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState)
    {
        super.onRestoreInstanceState(savedInstanceState);
        Log.e(TAG, "start onRestoreInstanceState~~~");

        // Restore UI state from the savedInstanceState.
        // This Bundle has also been passed to onCreate.
        // Will only be called if the Activity has been killed by the system
        // since it was last visible.
    }

    // Called at the start of the visible lifetime.
    @Override
    protected void onStart()
    {
        super.onStart();
        Log.e(TAG, "start onStart~~~");
        // Apply any required UI change now that the Activity is visible.
    }

    // Called before subsequent visible lifetimes for an Activity process.
    @Override
    protected void onRestart()
    {
        super.onRestart();
        Log.e(TAG, "start onRestart~~~");

        // Load changes knowing that the Activity has already been visible
        // within this process.
    }

    // Called at the start of the active lifetime
    @Override
    protected void onResume()
    {
        super.onResume();
        Log.e(TAG, "start onResume~~~");
        // Resume any paused UI updates, threads, or processes required
        // by the Activity but suspended when it was inactive.

    }

    // Called to save UI state changes at the end of the active lifecycle.
    @Override
    protected void onSaveInstanceState(Bundle outState)
    {
        super.onSaveInstanceState(outState);
        Log.e(TAG, "start onSaveInstanceState~~~");

        // Save UI state changes to the outState(savedInstanceState).
        // This Bundle will be passed to onCreate and onRestoreInstanceState if
        // the process is killed and restarted by the run time.
    }

    // Called at the end of the active lifetime.
    @Override
    protected void onPause()
    {
        super.onPause();
        Log.e(TAG, "start onPause~~~");
        // Suspend UI updates, threads, or CPU intensive processes that don't
        // need to be updated when the Activity isn't the active foreground
        // Activity.
    }

    // Called at the end of the visible lifetime.
    @Override
    protected void onStop()
    {
        super.onStop();
        Log.e(TAG, "start onStop~~~");

        // Suspend remaining UI updates, threads, or processing that aren't
        // required when the Activity isn't visible.
        // Persist all edits or state changes as after this call the process if
        // likely to be killed.
    }

    // Sometimes called at the end of the full lifetime.
    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        Log.e(TAG, "start onDestroy~~~");
        // Clean up any resources including ending threads, closing databases
        // connections etc.
    }

}
复制代码

转载于:https://www.cnblogs.com/myPersonalTailor/p/4975451.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
GeoPandas是一个开源的Python库,旨在简化地理空间数据的处理和分析。它结合了Pandas和Shapely的能力,为Python用户提供了一个强大而灵活的工具来处理地理空间数据。以下是关于GeoPandas的详细介绍: 一、GeoPandas的基本概念 1. 定义 GeoPandas是建立在Pandas和Shapely之上的一个Python库,用于处理和分析地理空间数据。 它扩展了Pandas的DataFrame和Series数据结构,允许在其中存储和操作地理空间几何图形。 2. 核心数据结构 GeoDataFrame:GeoPandas的核心数据结构,是Pandas DataFrame的扩展。它包含一个或多个列,其中至少一列是几何列(geometry column),用于存储地理空间几何图形(如点、线、多边形等)。 GeoSeries:GeoPandas中的另一个重要数据结构,类似于Pandas的Series,但用于存储几何图形序列。 二、GeoPandas的功能特性 1. 读取和写入多种地理空间数据格式 GeoPandas支持读取和写入多种常见的地理空间数据格式,包括Shapefile、GeoJSON、PostGIS、KML等。这使得用户可以轻松地从各种数据源中加载地理空间数据,并将处理后的数据保存为所需的格式。 2. 地理空间几何图形的创建、编辑和分析 GeoPandas允许用户创建、编辑和分析地理空间几何图形,包括点、线、多边形等。它提供了丰富的空间操作函数,如缓冲区分析、交集、并集、差集等,使得用户可以方便地进行地理空间数据分析。 3. 数据可视化 GeoPandas内置了数据可视化功能,可以绘制地理空间数据的地图。用户可以使用matplotlib等库来进一步定制地图的样式和布局。 4. 空间连接和空间索引 GeoPandas支持空间连接操作,可以将两个GeoDataFrame按照空间关系(如相交、包含等)进行连接。此外,它还支持空间索引,可以提高地理空间数据查询的效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值