Intent,可理解为信使,意图,通过它可以协助各个控件之间进行通讯,就等同于情侣之间所表达的爱意;在Android中,也将数据保存在Bundle对象中,就等同于撩妹高手的“情话”抽象和封装在一起;然后通过Intent提供的putExtras()方法将携带的数据(早保存在Bundle对象中的数据)保存在Intent对象中,这样便将”爱意”和”情话”相结合,在Activity之间运行复杂的逻辑,即这样才能完美告白。。。。。。
一、Intent实现页面跳转
1.startActivity(intent);
2.startActivityForResult(intent,requestCode);
a.在第一个Activity重写方法,接收回传数据
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
b.在第二个Activity重写方法,设置所要回传的数据
setResult(resultCode, data);
二、编写两个Activity来实现简单的页面跳转。
1.A_Activity.java和aactivity.xml代码
package com.oldtogether.intenttest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class A_Activity extends Activity {
private Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aactivity);
/*
* 1、通过点击btn1实现页面跳转
* 2、调用一个参数的startActivity方法实现页面跳转
*/
btn1 = (Button) findViewById(R.id.btn_first);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(A_Activity.this, B_Activity.class);
startActivity(intent);
}
});
}
}
<?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" >
<Button
android:id="@+id/btn_first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第一种启动方式" />
<Button
android:id="@+id/btn_second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第二种启动方式" />
<TextView
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="把第二个页面回传的数据显示出来"
android:textColor="#0FF"
android:textSize="32sp" />
</LinearLayout>
2.B_Activity.java和bactivity.xml代码
package com.oldtogether.intenttest;
import android.app.Activity;
import android.os.Bundle;
public class B_Activity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bactivity);
}
}
<?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" >
<Button
android:id="@+id/btn_back"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="返回"/>
</LinearLayout>
3.记得在AndroidManifest.xml文件中注册自定义的Activity,并将主接口给它。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.oldtogether.intenttest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".A_Activity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".B_Activity"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
4.运行结果
三、编写有返回值的页面跳转的代码
1.A_Activity.java和aactivity.xml代码
package com.oldtogether.intenttest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class A_Activity extends Activity {
private Button btn1;
private Button btn2;
private TextView tvShow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aactivity);
/*
* 1、通过点击btn1实现页面跳转
* 2、调用一个A_Activity.this参数的startActivity方法实现页面跳转
*/
tvShow = (TextView) findViewById(R.id.tv_show);
btn1 = (Button) findViewById(R.id.btn_first);
btn2 = (Button) findViewById(R.id.btn_second);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(A_Activity.this, B_Activity.class);
startActivity(intent);
}
});
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(A_Activity.this, B_Activity.class);
/*
* 1、第一个参数:Intent对象
* 2、第二个参数:请求的一个标识
*/
startActivityForResult(intent, 1);
}
});
}
/*
*通过startActivityForResult跳转,接收返回数据的方法
*requestCode:请求标识
*resultCode:第二个页面返回的标识
*data:第二个页面回传的数据(绑在意图对象中)
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==1 && resultCode==2){
String content=data.getStringExtra("data");
tvShow.setText(content);
}
}
}
<?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" >
<Button
android:id="@+id/btn_first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第一种启动方式" />
<Button
android:id="@+id/btn_second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第二种启动方式" />
<TextView
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hello,我是一个TextView!"
android:textColor="#f00"
android:textSize="32sp" />
</LinearLayout>
2.B_Activity.java和bactivity.xml代码
package com.oldtogether.intenttest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class B_Activity extends Activity{
private Button btnBack;
private String backContent="hi,这是来自第二个Activity的数据,并且返回到第二个页面。";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bactivity);
/*
* 1、什么时候给第二个页面回传数据,即返回点击按钮时
* 2、所返回的数据:backContent
* 3、将返回的数据绑在意图对象data中
* 4、requestCode:作为一个标识,需要和调用它的那个Activity对接
*/
btnBack = (Button) findViewById(R.id.btn_back);
btnBack.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent2 = new Intent();
intent2.putExtra("data", backContent);
setResult(2, intent2);
//关闭当前Activity,回顾关闭Activity的两种方法
finish();
}
});
}
}
<?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" >
<Button
android:id="@+id/btn_back"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="返回"/>
</LinearLayout>
3.同样在AndroidManifest.xml文件中注册,代码如下
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.oldtogether.intenttest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".A_Activity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".B_Activity"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
4.运行结果