startActivityForResult()的作用
相信很多人使用过startActivity(Intent intent)方法,调用该方法后,将会向目标activity跳转,但是该跳转只是单纯地跳转,并没有携带任何请求码。而startActivityForResult(Intent intent,int requestcode)的作用其实跟startActivity(Intent intent)相似,不同的是startActivityForResult()是带有请求码(请求码的作用主要是帮助activity判断来源)的跳转,跳转完成之后将会将requestcode传递给 onActivityResult(int requestCode,int resultCode,Intent data)。
startActivityForResult(Intent intent, int requestcode)
第一个参数Intent,和普通的startActivity()里的Intent一样,里面放要请求的Activity和可能需要放的数据。
第二个参数int,是一个请求代码,整型的,这个可以自己随便定义,但这个数要大于等于0才行。因为MainActivity有可能会跳转到多个页面,如果这几个页面使用完之后,都需要返回一些信息,那么就必须要有个标识符来表示返回来过的是哪个页面的信息。
setResult(int, Intent)
第一个参数int,是一个返回代码,整型的,这个也是自己随便定义,用来表示这个页面执行之后是个什么情况,是成功还是失败了,还是其它什么的,反正返回一个整型数,自己知道它的含义就行了。
第二个可选参数是一个Intent,可以用来存放数据
onActivityResult(int requestCode,int resultCode,Intent data)
这个方法是请求的Activity完成任务被finish()之后,会调用这个,前提是,你启动那个Activity是通过startActivityForResult()来启动的。
第一个参数int,是请求代码,就是startActivityForResult()里面的请求代码。
第二个参数int,是返回代码,就是setResult()方法里面的设置的参数。
第三个参数Intent,就是setResult(int, Intent)里面的放置数据的Intent。
下面看一段示例
mainactivity的java代码:
package com.example.returnrusltactivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
final int CODE=0x11; //定义一个校验码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.loginButton);
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Intent intent=new Intent();
EditText l1=(EditText)findViewById(R.id.maineditview1);
EditText l2=(EditText)findViewById(R.id.maineditview2);
Bundle bundle=new Bundle();
bundle.putCharSequence("user_name", l1.getText().toString()); //将两个编辑框的内容读取出来保存到bundle中
bundle.putCharSequence("password_name", l2.getText().toString());
intent.setClass(MainActivity.this,secondActivity.class );
intent.putExtras(bundle);
startActivityForResult(intent, 0x11); //跳转
}
});
}
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data) {
//super.onActivityResult(resultCode, requestCode, data);
if(requestCode==CODE && resultCode==CODE ) { //判断requestCode和resultCode是否符合条件
EditText e1=(EditText)findViewById(R.id.maineditview1);
EditText e2=(EditText)findViewById(R.id.maineditview2);
//Intent intent=getIntent();
Bundle bundle=data.getExtras();
e1.setText(bundle.getString("user")); //将第二个activity保存的数据填写到两个编辑框中
e2.setText(bundle.getString("password"));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
secondActivity的Java代码:
package com.example.returnrusltactivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class secondActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
Button r=(Button)findViewById(R.id.secondReturnButton);
final EditText e1=(EditText)findViewById(R.id.secondeditview1);
final EditText e2=(EditText)findViewById(R.id.secondeditview2);
Bundle bundle=new Bundle();
Intent i=getIntent();
bundle=i.getExtras();
String us=bundle.getString("user_name");
String ps=bundle.getString("password_name");
e1.setText(us); //将主activity编辑框的内容自动填写到该activity中
e2.setText(ps);
r.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Intent intent=new Intent();
Bundle bundle=new Bundle();
Intent i=getIntent();
bundle=i.getExtras();
String us=bundle.getString("user_name");
String ps=bundle.getString("password_name");
us=Integer.toString((Integer.parseInt(us))+1); //对主activity传输过来的内容进行处理,然后传递回主activity
ps=Integer.toString((Integer.parseInt(ps))+2);
bundle.putCharSequence("user", us);
bundle.putCharSequence("password",ps);
intent.putExtras(bundle);
setResult(0x11,intent);
finish();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
main的配置文件:
<?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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.returnrusltactivity.MainActivity"
android:orientation="vertical"
android:gravity="center_vertical">
<EditText android:id="@+id/maineditview1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:hint="@string/user"
/>
"
<EditText android:id="@+id/maineditview2"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:hint="@string/password"
/>
<Button android:id="@+id/loginButton"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="@string/login"/>"
</LinearLayout>
secondactivity的配置文件:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="0,3"
android:configChanges="orientation|screenSize|keyboardHidden">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/secondReturnButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/back"
android:background="#00000000"
android:gravity="top|start"
/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:id="@+id/secondTable1"
android:layout_weight="1">
<LinearLayout
android:id="@+id/secondLinear1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical" >
<EditText android:id="@+id/secondeditview1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:hint="@string/user"/>
<EditText android:id="@+id/secondeditview2"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:hint="@string/password"/>
<Button android:id="@+id/registerButton"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="@string/register"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"/>
</LinearLayout>
</TableRow>
</TableLayout>
程序演示:
