startActivity(intent):只是从当前活动界面跳转到另外一个界面,两个界面中不再有联系。
startActiivityForResult(requestcode,intnet):可以经当前活动界面认为是一个父窗体,要跳转的界面为子窗体,当子窗体关闭时,父窗体会执行onActivityResult()方法,并可以获取子窗体的返回值.
详细代码:
package net.lazyer.ActivityTest;
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;
/*
* create date:2010-12-13
* auther:huhqian
* description:联系Activity之间的跳转*/
public class ActivityTestMain extends Activity {
private Button btnToSubActivity=null;
private Button btnToOtherActivity=null;
private TextView txtInfo=null;
static int REQUEST_OK=1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FindViewByID();
//为按钮添加监听对象
btnToOtherActivity.setOnClickListener(setBtnListener);
btnToSubActivity.setOnClickListener(setBtnListener);
}
//获取layout中的定义的控件
private void FindViewByID(){
btnToOtherActivity=(Button)findViewById(R.id.btnToOtherActivity);
btnToSubActivity=(Button)findViewById(R.id.btnToSubActivity);
txtInfo=(TextView)findViewById(R.id.txtInfo);
}
//定义按钮的监听对象
private OnClickListener setBtnListener=new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Intent mIntent=new Intent();
switch (v.getId()) {
case R.id.btnToOtherActivity:
//直接跳转到其他界面
mIntent.setClass(ActivityTestMain.this,OtherActivity.class);
startActivity(mIntent);
break;
default:
mIntent.setClass(ActivityTestMain.this,SubActivity.class);
startActivityForResult(mIntent,REQUEST_OK);
break;
}
}
};
// 当SubActivity界面关闭时,执行该方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==REQUEST_OK){
if(resultCode==REQUEST_OK){
Bundle mBundle=data.getExtras();
if(mBundle!=null)
txtInfo.setText(mBundle.getString("activityInfo"));
}
}
}
}
该类文件对应的main.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/txtInfo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="hello,this is AcitvityTestMain"
/>
<Button
android:id="@+id/btnToSubActivity"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="to subActivity"
/>
<Button
android:id="@+id/btnToOtherActivity"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="to OtherActivity"
/>
</LinearLayout>
OtherActivity.java,与ActivityMain.java同级的活动界面:
package net.lazyer.ActivityTest;
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 OtherActivity extends Activity{
private Button btnClosed=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//加载相应的界面
setContentView(R.layout.otheractivity);
FindViewByID();
btnClosed.setOnClickListener(setBtnListener);
}
private void FindViewByID(){
btnClosed=(Button)findViewById(R.id.btnClose);
}
private OnClickListener setBtnListener=new OnClickListener(){
public void onClick(View v){
Intent mIntent=new Intent(OtherActivity.this,ActivityTestMain.class);
startActivity(mIntent);
}
};
}
对应的otheractivity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/txtOhterInfo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="this is otherActivity"
/>
<Button
android:id="@+id/btnClose"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="closed"
/>
</LinearLayout>
SubActivity.java,ActivityTestMain.java的子界面
package net.lazyer.ActivityTest;
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 SubActivity extends Activity{
private Button btnBack=null;
private TextView txtInfo=null;
static int RESULT_CODE=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.subactivity);
FindViewByID();
btnBack.setOnClickListener(setBtnListener);
}
private void FindViewByID(){
btnBack=(Button)findViewById(R.id.btnBack);
txtInfo=(TextView)findViewById(R.id.txtSubInfo);
}
private OnClickListener setBtnListener=new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Intent mIntent=new Intent();
Bundle mBundle=new Bundle();
mBundle.putString("activityInfo", txtInfo.getText().toString());
mIntent.putExtras(mBundle);
setResult(RESULT_CODE, mIntent);
finish();
}
};
}
对应的subactivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/txtSubInfo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="this is subActivity"
/>
<Button
android:id="@+id/btnBack"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Back"
/>
</LinearLayout>

浙公网安备 33010602011771号