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.
分享到:
相关推荐
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 ...
除了基本的调用Java方法外,JNI还允许创建自己的Android Activities和Services。这是因为有时候Qt框架中缺少某些API,或者Qt无法直接访问某些Android平台的特殊能力。在这种情况下,开发者可以利用JNI来创建完整的...
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"这个压缩包文件提供了一个关于如何实现Android聊天功能的示例。以下是根据标题、描述以及标签所涉及的主要知识点: 1. **Android Chat Activities**: 在Android...
在本项目中,"e_5_1-Chat-Activities.rar_android" 提供了一个使用Java语言开发的Android应用程序,其核心功能是实现一个简单的聊天应用。这个应用可能包含多个活动(Activities),每个活动对应于聊天界面的不同...
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...
1. **AndroidManifest.xml**:这是每个Android应用的核心配置文件,包含了应用的元数据,如应用的名称、权限、活动(Activities)、服务(Services)等组件的声明。 2. **MainActivity.java**:通常情况下,这是...
在移动互联网的,安卓应用...通过Robotium支持,可以写功能,系统和验收测试方案,跨越多个Android Activities。 近刚好在学习Robotium,下面是学习的心得以及使用过程中遇到的问题点记录下作为备忘,顺便大家一
在Android应用开发中,有效地管理和跟踪数据传递是至关重要的,特别是在Activities和Fragments之间的交互过程中。`Android-Robin`库就是为了满足这一需求而设计的,它是一个专门用于记录和调试Activities与Fragments...
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是构成应用程序的基本组件,它们代表了用户可见的屏幕界面。Activities之间的数据传递是Android开发中的常见任务,尤其是在多个Activity交互时。本文将详细介绍如何在Android的...
3. **AndroidManifest.xml**:每个Android应用都有一个重要的配置文件,用于声明应用所需的权限、活动(Activities)、服务(Services)等组件,以及它们之间的关系。 4. **Activity**:Android中的Activity是用户...
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
该层是应用程序开发中经常接触的部分,涉及的组件有activities、services、broadcast receivers等,这些组件构成了Android应用的基础架构。 五、其他目录结构介绍 除上述主要目录外,还有许多其他目录,分别承担着...
#### Activities 活动(Activity)是Android应用中用户进行交互的单一屏幕。文档详细讨论了创建和启动活动、活动的生命周期和状态,以及如何使用隐式意图(Implicit Intents)来启动其他活动。了解活动是构建复杂...
2. **应用程序框架**:Android 1.0提供了一个基于组件的服务框架,允许开发者轻松创建和集成各种应用服务,如活动(Activities)、服务(Services)、广播接收器(Broadcast Receivers)和内容提供者(Content ...
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...
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 ...
5. **活动(Activities)**:活动是Android应用的基本单元,负责用户界面交互。一个应用可以有多个活动,它们之间通过Intent进行通信。 6. **意图(Intents)**:Intent用于启动服务、启动或跳转到新的活动,以及在...