Activity跳转
Activity之间的跳转主要是使用Intent来实现,分为显示跳转和隐式跳转两种:
- 显示跳转是平时最常用的跳转方式,需要指明目标Activity的类名,并且只能调用应用内的Activity,不能跨应用启动Activity。
- 隐式跳转不需要明目标Activity的类名,而是需要指定目标Activity的一些特征,当有符合指定特征的Activity存在时便会被启动。并且隐式跳转是可以跨应用启动Activity的。
假如学校里每一个教室是一个APP,每个教室里的同学是一个Activity的话:
- 显示跳转就像在一个没有重名同学的教室里喊“王钢蛋同学请举手”,结果只有王钢蛋同学举手。
- 隐式跳转就像在广播室里喊“D罩杯的同学请举手”,结果整个教学楼D罩杯的女同学都听到了广播,因为符合条件的可能不止一个人所以举手的女同学可能有多个。
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.activitydemo" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".Activity1" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Activity2" >
</activity>
<activity android:name=".Activity3" android:theme="@style/Theme.AppCompat.Dialog" >
</activity>
</application>
</manifest>
Activity1.Java
package com.activitydemo;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Activity1 extends AppCompatActivity implements View.OnClickListener {
private Button buttonOpenActivity2, buttonOpenActivity3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_activity1);
buttonOpenActivity2 = (Button) findViewById(R.id.btn_act1_1);
buttonOpenActivity2.setOnClickListener(this);
buttonOpenActivity3 = (Button) findViewById(R.id.btn_act1_2);
buttonOpenActivity3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent;
switch(v.getId()) {
case R.id.btn_act1_1:
intent=new Intent(this, Activity2.class);
startActivity(intent);
break;
case R.id.btn_act1_2:
intent = new Intent("com.activitydemo.ACTION_START");
startActivity(intent);
break;
default:
break;
}
}
}
Activity2.Java
package com.activitydemo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class Activity2 extends AppCompatActivity implements View.OnClickListener {
private Button buttonOpenActivity1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_activity2);
buttonOpenActivity1 = (Button) findViewById(R.id.btn_act2_1);
buttonOpenActivity1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent=new Intent(this, Activity1.class);
startActivity(intent);
}
}
Activity3.Java
package com.activitydemo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class Activity3 extends AppCompatActivity implements View.OnClickListener {
private Button buttonOpenActivity1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_activity3);
buttonOpenActivity1 = (Button) findViewById(R.id.btn_act3_1);
buttonOpenActivity1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent=new Intent(this, Activity1.class);
startActivity(intent);
}
}
layout_activity1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn_act1_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="跳转到Activity2(显示跳转)"/>
<Button
android:id="@+id/btn_act1_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="跳转到Activity3(隐式跳转)"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical">
<TextView
android:text="Activity 1"
android:textSize="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"/>
</LinearLayout>
</LinearLayout>
layout_activity2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn_act2_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="返回Activity1"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical">
<TextView
android:text="Activity 2"
android:textSize="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"/>
</LinearLayout>
</LinearLayout>
layout_activity3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn_act3_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="返回Activity1"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical">
<TextView
android:text="Activity 3"
android:textSize="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"/>
</LinearLayout>
</LinearLayout>
在<action>
标签中我们指明了当前活动可以响应com.activitydemo.ACTION_START
这个action,而<category>
标签则包含了一些附加信息,更精确地指明了当前的活动能够响应的Intent中还可能带有的 category。每个Intent中只能指定一个action,但却能指定多个category。只有<action>
和<category>
中的内容同时能够匹配上,Intent中指定的action和category时才能跳转。 android.intent.category.DEFAULT
是一种默认的category,在调用startActivity()方法的时候会自动将这个category添加到Intent中。
利用隐式跳转的特点,我们可以在Activity中方便的调用各种系统程序和服务,例如可以按下面的方法修改Activity1.java:
1、打开网页
case R.id.btn_act1_2:
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.baidu.com"));
startActivity(intent);
break;
2、拨号
case R.id.btn_act1_2:
intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:13812345678"));
startActivity(intent);
break;
3、地图定位
case R.id.btn_act1_2:
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("geo:100,100"));
startActivity(intent);
break;
关于这部分Intent相关的内容,打算等详细学一下以后再详细写一下,网上已经有很多总结,可以先参考,比如:http://blog.csdn.net/wl455624651/article/details/7943252
Activity传值
Activity之间传递数据有下面这些方式:
- Intent
- Application context
- static 静态数据
- 外部存储
(1). 文件存储:File
(2). XML存储:Preference
(3). 数据库存储:Sqlite
(4). URI存储:Content Provider
这次先简单介绍一下Intent传值,Intent传值可以分为两种情况,
1、只传值,例如:Activity1传值给Activity2。
Activity1.java:传值给Activity2
Intent intent=new Intent(this, Activity2.class);
intent.putExtra("key1", "value1");
intent.putExtra("key2", 123);
startActivity(intent);
Activity2.java:取出Activity1传来的值
Intent intent = getIntent();
String strValue = intent.getStringExtra("key1");
int iValue = intent.getIntExtra("key2", 0);
2、传值并返回结果,例如:Activity1传值给Activity2,Activity2返回结果给Activity1
Activity1.java: 传值给Activity2
Intent intent=new Intent(this, Activity2.class);
intent.putExtra("key1", "value1");
intent.putExtra("key2", 123);
startActivity(intent);
Activity1.java: 取得Activity2的返回值
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
case 1:
if(resultCode == RESULT_OK) {
String strValue = data.getStringExtra("key1");
int iValue = data.getIntExtra("key2", 0);
}
break;
default:
break;
}
}
Activity2.java:取出Activity1传来的值
Intent intent = getIntent();
String strValue = intent.getStringExtra("key1");
int iValue = intent.getIntExtra("key2", 0);
Activity2.java:返回结果给Activity1
@Override
public void onClick(View v) {
Intent intent=new Intent(this, Activity1.class);
intent.putExtra("key1", "value2");
intent.putExtra("key2", 456);
setResult(RESULT_OK, intent);
finish();
}