`

Android activities

 
阅读更多
An application usually consists of multiple activities that are loosely bound to each other.

When an activity is stopped because a new activity starts, it is notified of this change in state through the activity's lifecycle callback methods.

For example, here's how one activity starts another activity named SignInActivity:
Intent intent = new Intent(this, SignInActivity.class);
startActivity(intent);

This is where intents are really valuable—you can create an intent that describes an action you want to perform and the system launches the appropriate activity from another application.
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, recipientArray);
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.
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...
        }
    }
}



An activity can exist in essentially three states:

Resumed
The activity is in the foreground of the screen and has user focus. (This state is also sometimes referred to as "running".)
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.
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.

Implementing the lifecycle callbacks
public class ExampleActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // The activity is being created.
    }
    @Override
    protected void onStart() {
        super.onStart();
        // The activity is about to become visible.
    }
    @Override
    protected void onResume() {
        super.onResume();
        // The activity has become visible (it is now "resumed").
    }
    @Override
    protected void onPause() {
        super.onPause();
        // Another activity is taking focus (this activity is about to be "paused").
    }
    @Override
    protected void onStop() {
        super.onStop();
        // The activity is no longer visible (it is now "stopped")
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // The activity is about to be destroyed.
    }
}

The entire lifetime of an activity happens between the call to onCreate() and the call to onDestroy().
The visible lifetime of an activity happens between the call to onStart() and the call to onStop().
The foreground lifetime of an activity happens between the call to onResume() and the call to onPause().

状态转换图:


Saving activity state


Note: Because onSaveInstanceState() is not guaranteed to be called, you should use it only to record the transient state of the activity (the state of the UI)—you should never use it to store persistent data. Instead, you should use onPause() to store persistent data (such as data that should be saved to a database) when the user leaves the activity.
分享到:
评论

相关推荐

    Professional Android 4 Application Development 源代码

    A Closer Look at Android Activities Chapter 4: Understanding Fragments Fundamental Android UI Design Android User Interface Fundamentals Introducing Layouts To-Do List Example Introducing Fragments ...

    Qt on Android with JNI

    除了基本的调用Java方法外,JNI还允许创建自己的Android Activities和Services。这是因为有时候Qt框架中缺少某些API,或者Qt无法直接访问某些Android平台的特殊能力。在这种情况下,开发者可以利用JNI来创建完整的...

    Android代码-Android-Twitter-Spike

    Matt and Tony's 3hr attempt at android activities, intents, layouts and backgrounding slow running tasks.

    e_5_1-Chat-Activities.rar_android_android chat

    "e_5_1-Chat-Activities.rar_android_android chat"这个压缩包文件提供了一个关于如何实现Android聊天功能的示例。以下是根据标题、描述以及标签所涉及的主要知识点: 1. **Android Chat Activities**: 在Android...

    e_5_1-Chat-Activities.rar_android

    在本项目中,"e_5_1-Chat-Activities.rar_android" 提供了一个使用Java语言开发的Android应用程序,其核心功能是实现一个简单的聊天应用。这个应用可能包含多个活动(Activities),每个活动对应于聊天界面的不同...

    Android代码-Android开发框架cyborg

    If you are an Android developer then you know the pain of an Android Application birth, the logs, the screens, the lists, view pagers, recyclers, adapters, activities, fragments, services, intents and...

    AndroidDriver-master_layers2x3_简单android源码_androidapp源码_android_

    1. **AndroidManifest.xml**:这是每个Android应用的核心配置文件,包含了应用的元数据,如应用的名称、权限、活动(Activities)、服务(Services)等组件的声明。 2. **MainActivity.java**:通常情况下,这是...

    基于Android 原生的Instruments Robotium 测试框架

    在移动互联网的,安卓应用...通过Robotium支持,可以写功能,系统和验收测试方案,跨越多个Android Activities。  近刚好在学习Robotium,下面是学习的心得以及使用过程中遇到的问题点记录下作为备忘,顺便大家一

    Android-Robin是Activities和fragments之间传递Bundle数据的日志记录库

    在Android应用开发中,有效地管理和跟踪数据传递是至关重要的,特别是在Activities和Fragments之间的交互过程中。`Android-Robin`库就是为了满足这一需求而设计的,它是一个专门用于记录和调试Activities与Fragments...

    Beginning Android 4 Application Development

    From using activities and intents and creating rich user interfaces to working with SMS, messaging APIs, and the Android SDK, what you need is here. Provides clear instructions backed by real-world ...

    Android实现Activities之间进行数据传递的方法

    在Android应用开发中,Activities是构成应用程序的基本组件,它们代表了用户可见的屏幕界面。Activities之间的数据传递是Android开发中的常见任务,尤其是在多个Activity交互时。本文将详细介绍如何在Android的...

    weibo4android新浪微博Android案例源码(Android 2.2以上)

    3. **AndroidManifest.xml**:每个Android应用都有一个重要的配置文件,用于声明应用所需的权限、活动(Activities)、服务(Services)等组件,以及它们之间的关系。 4. **Activity**:Android中的Activity是用户...

    Android代码-Android Picture in Picture 效果 Demo。

    Android PictureInPicture ...As of Android O, activities can launch in Picture-in-Picture (PiP) mode. PiP is a special type of multi-window mode mostly used for video playback. The app is paused when it

    Android源码地图 ——Android源代码结构分析

    该层是应用程序开发中经常接触的部分,涉及的组件有activities、services、broadcast receivers等,这些组件构成了Android应用的基础架构。 五、其他目录结构介绍 除上述主要目录外,还有许多其他目录,分别承担着...

    Android官方培训教程

    #### Activities 活动(Activity)是Android应用中用户进行交互的单一屏幕。文档详细讨论了创建和启动活动、活动的生命周期和状态,以及如何使用隐式意图(Implicit Intents)来启动其他活动。了解活动是构建复杂...

    Android Design Patterns and Best Practices

    You will then develop an application that will help you grasp Activities, Services and Broadcasts and their roles in Android development. Moving on, you will add user detecting classes ansd APIs such...

    Android代码-android-disposebag

    Even if we could achieve this, there's still a problem with the Android platform, Activities are created and managed by the system, using it after either onDestroy or onStop method is called will ...

    android 离线帮助文档

    5. **活动(Activities)**:活动是Android应用的基本单元,负责用户界面交互。一个应用可以有多个活动,它们之间通过Intent进行通信。 6. **意图(Intents)**:Intent用于启动服务、启动或跳转到新的活动,以及在...

    ArcGIS Runtime SDK for Android v1.1

    3、支持单个应用程序中多个MapView,您可以在支持muti-mapview的单个应用程序中自由切换activities; 4、Group图层支持所有的图层类型,包括Graphics layer,并且可控制子图层的顺序、透明度和可见性; 5、支持web ...

Global site tag (gtag.js) - Google Analytics