您的位置:首页 > 其它

startActivity与startActivityForResult的区别

2012-08-08 14:46 411 查看
StartActivity

通过这种方式启动应用程序可以看作为独立运行于系统中,启动它的parent无需得到其所运行的返回值,更具体的讲是一个Standalone的应用程序启动了另一个有着同样Standalone特性的程序。

StartActivityForResult

通过方法的命名可以很直观的看到,其最大的作用是当启动了某个Activity后,parent依然关联着当前进程所处理的Activity。当操作完成 后,会有特定的返回值作为Parent响应某些Events的结果。即使用startActivityForResult方法,会在另外一个 Activity执行finish()以后执行回调方法onActivityResult,而使用startActivity方法却不会执行回调。

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 ActivityTestMain extends Activity {

private Button btnToSubActivity=null;
private Button btnToOtherActivity=null;
private TextView txtInfo=null;
static int REQUEST_OK=1;
@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界面关闭时,执行该方法,这里是startActivityForResult方法返回结果的地方

@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"));
}
}
}
}


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>


<?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 的实现部分


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);
}
};

}


SubActivity实现部分


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();
}
};

}


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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: