Android开发—Activity中的数据传递
putExtra()方法
在android中用intent传递数据一般会用 intent.putExtra() 来存放数据,在另一个Activity页面通过get××Extra()来获取数据。
putExtra(“A”,B)中,A B为键值对,A为键名,B为值。在你的另一个Activity中用get××Extra ()方法时,需要键对应。键就像钥匙,一把钥匙配一把锁。
示例:
数据传递:
在Activity2中放入数据传递给MainActivity:
Intent intent = new Intent(this,MainActivity.class);
intent.putExtra("Doctor_xiong", "Hello MainActivity");
startActivity(intent);
在MainActivity中获取Activity2传递来的数据。
Intent intent = getIntent();
String data = intent.getStringExtra("Doctor_xiong");
startActivity() 与 startActivityForResult()区别:
若只是从当前活动界面跳转到另外一个界面,两个界面中不再有联系,则startActivity(intent);
若当前活动界面认为是一个父窗体,要跳转的界面为子窗体,并且当前窗体还想获取跳转后子窗体的返回值,则startActivityForResult(intent,REQ_CODE);
startActivityForResult()方法详解:
使用startActivityForResult方法开启新的Activity2,第1个参数是Intent对象,第2个参数是请求码,用于判断数据的来源输入一个唯一值:
Intent intent = new Intent(this,Activity2.class);
startActivityForResult(intent,REQ_CODE);
finish()方法详解:
关闭界面的方法finish(),通常用子窗口的该实例方法来关闭子窗口,会依次执行子窗口几个方法:onPause(),onStop(),onDestory(),子窗口的onDestory()还未执行,而父窗口的onResume()方法已经执行了。
实战练习:
做两个页面,通过按钮点击将数据从一个页面传递至另一个页面。
一、在两个界面中布局两个按钮:
MainActivity布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.getdataback.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="当前窗口是Main,点击跳转到子页面"
android:onClick="onclick_main"/>
</LinearLayout>
Activity2布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.getdataback.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="当前窗口是Activity2,用于传递数据"
android:onClick="onclick_Activity2"/>
</LinearLayout>
二、【MainActivity】中添加按钮响应代码及onActivityResult方法处理代码:
package com.example.getdataback;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
final static int REQ_CODE=1;
final static int RES_CODE=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v("Doctor_xiong", "MainActivity 调用 onCreat()方法");
}
public void onclick_main(View view) {
Log.v("Doctor_xiong", "MainActivity: onclick_main");
Intent intent = new Intent(this, Activity2.class);
startActivityForResult(intent,REQ_CODE);
}
@Override
protected void onActivityResult(int requestCode,int resultCode,@Nullable Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==REQ_CODE){
if(resultCode==RES_CODE){
String string=data.getStringExtra("Doctor_xiong");
Log.v("Doctor_xiong","获取的位置模拟数据为:"+string);
}
}
}
@Override
protected void onStart(){
super.onStart();
Log.v("Doctor_xiong","MainActivity onStart()方法");
}
@Override
protected void onResume() {
super.onResume();
Log.v("Doctor_xiong","MainActivity onResume()方法");
}
@Override protected void onRestart()
{
super.onRestart();
Log.v("Doctor_xiong", "MainActivity onRestart()方法");
}
@Override
protected void onStop() {
super.onStop();
Log.v("Doctor_xiong","MainActivity onStop()方法");
}
@Override
protected void onPause() {
super.onPause();
Log.v("Doctor_xiong","MainActivity onPause()方法");
}
@Override protected void onDestroy() {
super.onDestroy();
Log.v("Doctor_xiong", "MainActivity onDestr");
}
}
三、【Activity2.java】:
package com.example.getdataback;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
public class Activity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Activity2);
Log.v("Doctor_xiong", "Activity2调用 onCreat()方法");
}
public void onclick_Activity2(View view){
Log.v("Doctor_xiong", "Activity2: onclick_Activity2");
Intent intent=new Intent(this,MainActivity.class);
intent.putExtra("Doctor_xiong","位置:中国北京");
setResult(MainActivity.RES_CODE,intent);
Log.v("Doctor_xiong", "Activity2 调用 finish()方法");
finish();
}
@Override
protected void onStart(){
super.onStart();
Log.v("Doctor_xiong","Activity2 onStart()方法");
}
@Override
protected void onResume() {
super.onResume();
Log.v("Doctor_xiong","Activity2 onResume()方法");
}
@Override protected void onRestart()
{
super.onRestart();
Log.v("Doctor_xiong", "Activity2 onRestart()方法");
}
@Override
protected void onStop() {
super.onStop();
Log.v("Doctor_xiong","Activity2 onStop()方法");
}
@Override
protected void onPause() {
super.onPause();
Log.v("Doctor_xiong", "ChildActivity onPause()方法");
}
@Override protected void onDestroy()
{
super.onDestroy();
Log.v("Doctor_xiong", "Activity2 onDestroy()方法");
}
}