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

    

    当一个Activity被载入创建的时候经历一下三个步骤
    onCreate
    onStart
    onResume
    当一个界面对用户可见,可是不能进行相关操作时,这个界面就处在 onPause的状态
    当一个界面处在对用户全然不可见的状态,该界面就处于onStop的状态 onPause -->onStop
    onCreate()<---->onDestroy()
    onStart()<----->onStop()
    onResume()<----->onPause()
    onRestart()界面处于onPause状态,又又一次获得焦点时调用该方法
    Activity的生命周期
    1.完整的生命周期( entire lifetime ):
    onCreate()-->onStart()-->onResume()-->onPuse()-->onStop()-->onDestroy()
    2.可视生命周期(visible lifetime):
    onStart()-->onResume()-->onPause()-->onStop()
    3.前台生命周期(foreground lifetime):
    onResume()<-->onPause()
    Activity的生命周期图解

    创建一个Activity,必须编写一个类继承Activity或者是继承Activity的子类。


    在Activity中,最重要的两个方法是:
      onCreate() 该方法是每个Activity都必须事先的方法。当Activity被创建的时候,系统会自己主动调用该方法。在该方法中,你必须初始化你的组件。在该方法中,最重要的一件事情就是调用setContentView()方法载入你定义好的视图组建。

      onPause()当用户离开正在使用的Activity时,该方法会被系统自己主动调用,用户离开一个Activity,并不代表该Activity被销毁了。 一般在该方法中进行提交和保存用户的数据和状态,以便用户再次回到该界面的时候,展示用户的数据。


    必须在manifest.xml文件里配置,系统才可以让您使用它。


    You must declare your activity in the manifest file in order for it to be accessible to the system. To declare your activity, open your manifest file and add an element as a child of the element. For example:
    <manifest ... >
     <application>
      <activity android:name=".ExampleActivity" />
      ...
     </application  >
     ...
    </manifest >
    An element can also specify various intent filters—using the <intent-filter> element—in order to declare how other application components may activate it.
    <activity android:name=".ExampleActivity" android:icon="@drawable/app_icon">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>
    让别的应用程序使用本程序的Activity的方法:
    if you want your activity to respond to implicit intents that are delivered from other applications (and your own), then you must include an <intent-filter> that includes an element and, optionally, a element and/or a element. These elements specify the type of intent to which your activity can respond.
    Starting an Activity
    You can start another activity by calling startActivity(), passing it an Intent that describes the activity you want to start.
    An intent can also carry small amounts of data to be used by the activity that is started.
    you want to start, using the class name such as
    Intent intent = new Intent(this, SignInActivity.class);
    startActivity(intent);
    Starting an activity for a result
    Sometimes, you might want to receive a result from the activity that you start. In that case, start the activity by calling startActivityForResult() (instead of startActivity()). To then receive the result from the subsequent activity, implement the onActivityResult() callback method. When the subsequent activity is done, it returns a result in an Intent to your onActivityResult() method.
    For example, perhaps you want the user to pick one of their contacts, so your activity can do something with the information in that contact. Here's how you can create such an intent and handle the result:
    private void pickContact() {
    // Create an intent to "pick" a contact, as defined by the content provider URI
    Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
    startActivityForResult(intent, PICK_CONTACT_REQUEST);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // If the request went well (OK) and the request was PICK_CONTACT_REQUEST
    if (resultCode == Activity.RESULT_OK && requestCode == PICK_CONTACT_REQUEST) {
        // Perform a query to the contact's content provider for the contact's name
        Cursor cursor = getContentResolver().query(data.getData(),
        new String[] {Contacts.DISPLAY_NAME}, null, null, null);
        if (cursor.moveToFirst()) { // True if the cursor is not empty
            int columnIndex = cursor.getColumnIndex(Contacts.DISPLAY_NAME);
            String name = cursor.getString(columnIndex);
            // Do something with the selected contact's name...
        }
    }
    }
    Shutting Down an Activity
    You can shut down an activity by calling its finish() method. You can also shut down a separate activity that you previously started by calling finishActivity().
    Activity的三种状态:
    1.Resumed
    The activity is in the foreground of the screen and has user focus. (This state is also sometimes referred to as "running".)
    2.Paused
    Another activity is in the foreground and has focus, but this one is still visible. That is, another activity is visible on top of this one and that activity is partially transparent or doesn't cover the entire screen. A paused activity is completely alive (the Activity object is retained in memory, it maintains all state and member information, and remains attached to the window manager), but can be killed by the system in extremely low memory situations.
    3.Stopped
    The activity is completely obscured by another activity (the activity is now in the "background"). A stopped activity is also still alive (the Activity object is retained in memory, it maintains all state and member information, but is not attached to the window manager). However, it is no longer visible to the user and it can be killed by the system when memory is needed elsewhere.
    If an activity is paused or stopped, the system can drop it from memory either by asking it to finish (calling its finish() method), or simply killing its process. When the activity is opened again (after being finished or killed), it must be created all over.
    任务栈task stack:用来记录用户的行为,与用户交互的界面
    栈:后进先出
    队列:先进先进先出
    每个应用程序都有自己的一个task stack
    任务栈的四种启动模式:
    1.standard (the default mode)
    2.singleTop
    3.singleTask
    4.singleInstance

  • 相关阅读:
    高通平台msm8953 Linux DTS(Device Tree Source)设备树详解之二(DTS设备树匹配过程)
    Linux DTS(Device Tree Source)设备树详解之一(背景基础知识篇)
    ACPI Table 与 Device Tree
    Electron 的 安装
    adb连接安卓设备失败failed to start daemon
    Linux greybus
    Android安卓版本、API级别和Linux内核对应关系
    「转」Android编译选项中的eng、user、user-debug
    高通SOC启动流程
    Android for MSM Project
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/7290048.html
Copyright © 2011-2022 走看看