Activity 详解

An Activity is an application component that provides a screen with which users can interact in order to do something, such as dial the phone, take a photo, send an email, or view a map. Each activity is given a window in which to draw its user interface. The window typically fills the screen, but may be smaller than the screen and float on top of other windows.

An application usually consists of multiple activities that are loosely bound to each other. Typically, one activity in an application is specified as the "main" activity, which is presented to the user when launching the application for the first time. Each activity can then start another activity in order to perform different actions. Each time a new activity starts, the previous activity is stopped, but the system preserves the activity in a stack (the "back stack"). When a new activity starts, it is pushed onto the back stack and takes user focus. The back stack abides to the basic "last in, first out" queue mechanism, so, when the user is done with the current activity and presses the BACK key, it is popped from the stack (and destroyed) and the previous activity resumes. (The back stack is discussed more in the Tasks and Back Stack document.)

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. There are several callback methods that an activity might receive, due to a change in its state—whether the system is creating it, stopping it, resuming it, or destroying it—and each callback provides you the opportunity to perform specific work that's appropriate to that state change. For instance, when stopped, your activity should release any large objects, such as network or database connections. When the activity resumes, you can reacquire the necessary resources and resume actions that were interrupted. These state transitions are all part of the activity lifecycle.

The rest of this document discusses the basics of how to build and use an activity, including a complete discussion of how the activity lifecycle works, so you can properly manage the transition between various activity states.

更详细的说明,请查阅官方的相关文档。
Activity有那么的状态,主要是为了提高速度,因为在不同状态之间切换,需要回调不同方法
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Java 中,Activity 是 Android 应用程序的核心组件之一。它代表了用户与应用程序交互的一个屏幕,类似于 Windows 中的窗口。Activity 提供了一个用户界面,用于显示应用程序的内容,并处理用户输入。在本文中,我们将详细介绍 Java 中的 Activity。 1. Activity 的生命周期 Activity 的生命周期是指从创建到销毁的整个过程。Activity 的生命周期可以分为以下几个部分: 1.1. 创建阶段 在创建阶段,Activity 将完成以下几个步骤: 1.1.1. onCreate():当 Activity 被创建时,系统会调用 onCreate() 方法。在此方法中,开发者可以初始化 Activity 的布局、绑定事件等。 1.1.2. onStart():当 Activity 准备好与用户进行交互时,系统会调用 onStart() 方法。在此方法中,开发者可以完成一些初始化工作,例如开始动画。 1.1.3. onResume():当 Activity 成为用户焦点并可以与用户进行交互时,系统会调用 onResume() 方法。在此方法中,开发者可以开始处理用户输入事件。 1.2. 运行阶段 在运行阶段,Activity 将完成以下几个步骤: 1.2.1. onPause():当 Activity 失去焦点并不再与用户进行交互时,系统会调用 onPause() 方法。在此方法中,开发者可以停止处理用户输入事件。 1.2.2. onStop():当 Activity 不再可见时,系统会调用 onStop() 方法。在此方法中,开发者可以完成一些清理工作,例如停止动画。 1.3. 销毁阶段 在销毁阶段,Activity 将完成以下几个步骤: 1.3.1. onDestroy():当 Activity 被销毁时,系统会调用 onDestroy() 方法。在此方法中,开发者可以完成一些清理工作,例如释放资源。 2. Activity 的启动方式 在 Java 中,可以通过以下方式启动一个 Activity: 2.1. 显式启动 显式启动是指通过明确指定要启动的 Activity 的类名来启动 Activity。例如: ```java Intent intent = new Intent(this, AnotherActivity.class); startActivity(intent); ``` 2.2. 隐式启动 隐式启动是指通过指定 Action、Category、Data 等信息来启动 Activity,系统将根据这些信息匹配合适的 Activity。例如: ```java Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setData(Uri.parse("http://www.example.com")); startActivity(intent); ``` 3. Activity 的传值 在 Java 中,可以通过 Intent 对象来传递数据到另一个 Activity。具体步骤如下: 3.1. 在发送 Intent 时,通过 putExtra() 方法来添加数据: ```java Intent intent = new Intent(this, AnotherActivity.class); intent.putExtra("name", "张三"); intent.putExtra("age", 18); startActivity(intent); ``` 3.2. 在接收 Intent 时,通过 getIntent() 方法获取 Intent 对象,并通过 getXXXExtra() 方法获取数据: ```java Intent intent = getIntent(); String name = intent.getStringExtra("name"); int age = intent.getIntExtra("age", 0); ``` 4. Activity 的返回值 在 Java 中,可以通过 startActivityForResult() 方法启动一个 Activity,并在该 Activity 中设置 setResult() 方法来返回数据。具体步骤如下: 4.1. 在发送 Intent 时,通过 startActivityForResult() 方法启动另一个 Activity: ```java Intent intent = new Intent(this, AnotherActivity.class); startActivityForResult(intent, REQUEST_CODE); ``` 4.2. 在接收 Intent 时,通过 onActivityResult() 方法获取返回的数据: ```java @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) { String result = data.getStringExtra("result"); } } ``` 4.3. 在被返回的 Activity 中,通过 setResult() 方法设置返回的数据: ```java Intent intent = new Intent(); intent.putExtra("result", "返回数据"); setResult(RESULT_OK, intent); finish(); ``` 以上就是 Java 中 Activity详解,希望对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值