本文内容 1. 使用 Activity 显式简单界面 2. Activity 之间的跳转 2.1 startActivity 2.3 startActivityForResult 3. Activity 之间的数据交互 4. Activity 的启动 4.1 显示启动 4.2 隐式启动 5. 启动系统中常见的 Activity 5.1 打开浏览器网页 5.2 打开相册 5.3 打开发送短信界面 5.4 打开拨号界面 6. 结束 Activity
package com.zhangmiao.activityproject; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/main_activity_message_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:freezesText="true" android:text="这是 MainActivity" /> </LinearLayout>
<activity android:name=".MainActivity" android:configChanges="orientation|screenSize" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
经过调用 startActivity() 方法能够启动其余 Activity,在 Intent 中指定想要启动的 Activity ,Intent 对象还可能携带少许数据传递给想要启动的 Activity。html
Intent firstIntent = new Intent(MainActivity.this, FirstActivity.class); //携带数据 firstIntent.putExtra("from", "MainActivity"); startActivity(firstIntent);
有时,可能须要从启动的 Activity 得到结果。在这种状况下,就经过 startActivityForResult() 方法来启动 Activity ,被启动的 Activity 经过 setResult() 方法返回数据,启动的 Activity 经过 onActivityResult() 方法获取返回的数据。android
简单的 MainActivity 跳转 FirstActivity 界面,FirstActivity 返回数据给 MainActivityweb
Intent firstIntent = new Intent(MainActivity.this, FirstActivity.class); firstIntent.putExtra("from", "MainActivity"); startActivityForResult(firstIntent,10001);
FirstActivity 在合适的位置返回数据给 MainActivity数组
Intent intent = new Intent(); intent.putExtra("result","OK"); setResult(20001,intent);
MainActivity 接收 FirstActivity 返回的数据浏览器
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 10001 && resultCode == 20001){ if (data != null) { String result = data.getStringExtra("result"); } } }
可使用 Intent 对象进行数据的传递。Intent 重载了不少 putExtra() 方法,包括了 Java 八大基本类型以及其数组类型等。app
Intent firstIntent = new Intent(MainActivity.this, FirstActivity.class); firstIntent.putExtra("from", "MainActivity"); startActivityForResult(firstIntent,10001);
Intent 对象还有一个 putExtras(Bundle bundle) 方法,就是把须要传递的数据组合在一块儿进行传递。ide
Intent firstIntent = new Intent(MainActivity.this, FirstActivity.class); Bundle bundle = new Bundle(); bundle.putString("from", "MainActivity"); bundle.putInt("count", 1); firstIntent.putExtras(bundle); startActivityForResult(firstIntent,10001);
显示启动的方式:post
直接在 Intent 构造方法启动:this
Intent firstIntent = new Intent(MainActivity.this, FirstActivity.class);
setComponentspa
ComponentName componentName = new ComponentName(this,FirstActivity.class); Intent firstIntent = new Intent(); firstIntent.setComponent(componentName); startActivity(firstIntent);
setClass / setClassName
Intent firstIntent = new Intent(); firstIntent.setClass(this,FirstActivity.class); firstIntent.setClassName(this,"com.zhangmiao.activityproject.FirstActivity"); startActivity(firstIntent);
隐式启动就是要在该 Activity 中设置 IntentFilter 属性,只要启动的 Intent 匹配 IntentFilter 的条件就能够启动相应的 Activity 。
对于想要做出响应的每个 Intent 类型,必须加入相应的 < intent-filter >,其中包括一个 < action > 元素,还可选择性地包括一个 < category > 元素或一个 < data > 元素。这些元素指定 activity 能够响应的 Intent 类型。
Intent intent = new Intent(); intent.setAction(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); startActivity(intent);
系统给咱们提供了不少经常使用的 Activity,能够用来打开浏览器,打开发短信界面,打开相册界面,打开拨号界面等等。
Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setData(Uri.parse("http://www.baidu.com")); startActivity(intent);
Intent intent = new Intent(); intent.setAction(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); startActivity(intent);
Intent intent = new Intent(); intent.setAction(Intent.ACTION_SNED); intent.setType("text/plain"); intent.putExtra(Intent.EXTRA_TEXT,"Hello World!"); startActivity(intent);
Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setData(Uri.parse("tel:110")); startActivity(intent);
能够经过调用 Activity 的 finish() 方法来结束 Activity 。还能够经过调用 finishActivity() 方法结束以前启动的另外一个 Activity 。
大多数状况下,不该该使用 finish() 或 finishActivity() 方法来结束 Activity 。Android 系统会管理 Activity 的生命周期,会在合适的时机结束 Activity 。调用结束 Activity 的方法可能对预期的用户体验产生不良影响,所以只应该在确实不想让用户返回此 Activity 实例时使用。