[/url]
[b]1.无参数Activity跳转[/b]
Intent it = new Intent(Activity.Main.this, Activity2.class);
startActivity(it);
[b]2.向下一个Activity传递数据(使用Bundle和Intent.putExtras)[/b]
Intent it = new Intent(Activity.Main.this, Activity2.class);
Bundle bundle=new Bundle();
bundle.putString("name", "This is from MainActivity!");
it.putExtras(bundle); // it.putExtra(“test”, "shuju”);
startActivity(it); // startActivityForResult(it,REQUEST_CODE);
对于数据的获取可以采用:
Bundle bundle=getIntent().getExtras();
String name=bundle.getString("name");
或者:
Intent intent = getIntent();
intent.setClass(MyActivity.this,ForwardTarget.class);
intent.putExtra("param1","Pandy");
intent.putExtra("param2","GuiZhou");
startActivityForResult(intent, GET_CODE);
//获取参数,并显示。
Intent intent = getIntent();
String param1 = intent.getStringExtra("param1");
String param2 = intent.getStringExtra("param2");
[b]3.向上一个Activity返回结果(使用setResult,针对[/b]
startActivityForResult(it,REQUEST_CODE)启动的Activity)
Intent intent=getIntent();
Bundle bundle2=new Bundle();
bundle2.putString("name", "This is from ShowMsg!");
intent.putExtras(bundle2);
setResult(RESULT_OK, intent);
[b]4.回调上一个Activity的结果处理函数(onActivityResult)[/b]
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==REQUEST_CODE){
if(resultCode==RESULT_CANCELED)
setTitle("cancle");
else if (resultCode==RESULT_OK) {
String temp=null;
Bundle bundle=data.getExtras();
if(bundle!=null) temp=bundle.getString("name");
setTitle(temp);
}
}
}
[color=red][b]注意:[/b][/color]
总结:调用startActivityForResult,onActivityResult无响应的问题 [url]http://blog.sina.com.cn/s/blog_5da93c8f0101gzf5.html[/url]
我碰到问题跟这里的不一样,就是在接受返回参数的Activity里面的一些方法,使用了getIntent()来得到Intent,导致还没运行返回参素好的Activity,就已经调用了onActivityResult();
[color=red]最好不要在非onCreate()方法外使用getIntent()来获取Intent[/color];
[color=red]我的例子源码:[/color]
main.xml
----------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/factorone"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/symbol"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/symbol" />
<EditText
android:id="@+id/factortwo"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/calculate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/calculate" />
</LinearLayout>
result.xml
----------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/totalRsult"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/symbol" />
</LinearLayout>
strings.xml
----------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Android_TwoActivityActivity!</string>
<string name="app_name">Android_TwoActivity</string>
<string name="firstActivity">第一个Activity</string>
<string name="secondActivity">第二个Activity</string>
<string name="symbol">乘以</string>
<string name="calculate">计算</string>
</resources>
FirstActivity.java
----------------------------------------------
package com.pandy.test;
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.EditText;
import android.widget.TextView;
public class FirstActivity extends Activity {
private EditText factorOne;
private EditText factorTwo;
private TextView symbol;
private Button calculate;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
factorOne = (EditText) findViewById(R.id.factorone);
factorTwo = (EditText) findViewById(R.id.factortwo);
symbol = (TextView) findViewById(R.id.symbol);
calculate = (Button) findViewById(R.id.calculate);
calculate.setOnClickListener(new MyListener());
}
class MyListener implements OnClickListener {
@Override
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setClass(FirstActivity.this, SecondActivity.class);
intent.putExtra("factorOneStr", factorOne.getText().toString());
intent.putExtra("factorTwoStr", factorTwo.getText().toString());
startActivity(intent);
}
}
}
SecondActivity.java
----------------------------------------------
package com.pandy.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends Activity {
private TextView resultView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
resultView = (TextView) findViewById(R.id.totalRsult);
Intent intent = getIntent();
String factorOneStr = intent.getStringExtra("factorOneStr");
String factorTwoStr = intent.getStringExtra("factorTwoStr");
int factorOneInt = Integer.parseInt(factorOneStr);
int factorTwoInt = Integer.parseInt(factorTwoStr);
resultView.setText(factorOneInt * factorTwoInt + "");
}
}
R.java
----------------------------------------------
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package com.pandy.test;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int ic_launcher=0x7f020000;
}
public static final class id {
public static final int calculate=0x7f050003;
public static final int factorone=0x7f050000;
public static final int factortwo=0x7f050002;
public static final int symbol=0x7f050001;
public static final int totalRsult=0x7f050004;
}
public static final class layout {
public static final int main=0x7f030000;
public static final int result=0x7f030001;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int calculate=0x7f040005;
public static final int firstActivity=0x7f040002;
public static final int hello=0x7f040000;
public static final int secondActivity=0x7f040003;
public static final int symbol=0x7f040004;
}
}
AndroidManifest.xml
----------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pandy.test"
android:versionCode="1"
android:versionName="1.0" >
<application
android:label="@string/app_name" >
<activity
android:name=".FirstActivity"
android:label="@string/firstActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="@string/secondActivity" />
</application>
<uses-sdk android:minSdkVersion="4" />
</manifest>