startActivity与startActivityForResult的区别

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>

转载于:https://www.cnblogs.com/Tammie/archive/2012/08/08/2628249.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值