startActivityForResult 使用详解

Starting Activities and Getting Results

如果,要想达到这个效果,你需要调用startActivityForResult(Intent, int) (这个method的第二个参数描定义了这个回调)。当返回的时候,这个activity将要调用onActivityResult(int, int, Intent) 。
如果这个activity存在,它能够调用setResult(int) 返回数据给 调用它的父activity。它必须的有一个返回码,这个返回码可以是ESULT_CANCELED, RESULT_OK, or any custom values starting at RESULT_FIRST_USER。
如果,调用的child activity失败,这个parent activity 将会收到的返回码是RESULT_CANCELED.

 public class MyActivity extends Activity {
     ...

     static final int PICK_CONTACT_REQUEST = 0;

     protected boolean onKeyDown(int keyCode, KeyEvent event) {
         if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
             // When the user center presses, let them pick a contact.
             startActivityForResult(
                 new Intent(Intent.ACTION_PICK,
                 new Uri("content://contacts")),
                 PICK_CONTACT_REQUEST);
            return true;
         }
         return false;
     }

     protected void onActivityResult(int requestCode, int resultCode,
             Intent data) {
         if (requestCode == PICK_CONTACT_REQUEST) {
             if (resultCode == RESULT_OK) {
                 // A contact was picked.  Here we will just display it
                 // to the user.
                 startActivity(new Intent(Intent.ACTION_VIEW, data));
             }
         }
     }
 }
 



一个转载的Demo

activity1
package com.android.ActivityGo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

public class Activity1 extends Activity {
	EditText first,second;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main1);
        
        first = (EditText) findViewById(R.id.first);
        second = (EditText) findViewById(R.id.second);
        
        findViewById(R.id.start).setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sendCalculate();
			}
        });
    }
    
    public void sendCalculate(){
    	Intent i = new Intent(this, Activity2.class);
    	
    	Bundle b = new Bundle();
    	
    	b.putString("first", first.getText().toString());
    	b.putString("second", second.getText().toString());
    	
    	i.putExtras(b);
    	
    	startActivityForResult(i,10);
    	
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode,
                                    Intent data){
//    	10  -1
    	System.out.println(requestCode+">>>>"+resultCode);
    	setTitle(requestCode+">>>>"+resultCode);
    	switch (resultCode){
    	case RESULT_OK:
//    		Bundle b = data.getExtras();
    		
//    		String string = b.getString("CALCULATION");
    		
    		updateText("55555555555555");
    	}
    }
    
    public void updateText(String s){
    	TextView t = (TextView) findViewById(R.id.text);
    	t.setText(s);
    }
}

activity2


package com.android.ActivityGo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

public class Activity2 extends Activity {
    String value;
    
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
        
        
        Intent i = this.getIntent();
        
        Bundle b = i.getExtras();
        
        String v1 = b.getString("first");
        String v2 = b.getString("second");
        
        value = v1 + v2;
        
        findViewById(R.id.reply).setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				// TODO Auto-generated method stub
				replyCalculation();
			}
        });
        
    }
    
    
    public void replyCalculation(){
    	Intent i = new Intent();
    	
    	Bundle b = new Bundle();
    	b.putString("CALCULATION", value);
    	
    	i.putExtras(b);
    	
    	this.setResult(RESULT_OK);
    	this.finish();
    	
    }
}

一个小Demo

主界面:
public class StartResultTestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button=(Button) findViewById(R.id.button1);
        Button button2=(Button) findViewById(R.id.button2);
//        Intent intent=new Intent(this, cls)
        button.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivityForResult(new Intent(StartResultTestActivity.this, Activity1.class), 1);
			
			}
		});
        button2.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivityForResult(new Intent(StartResultTestActivity.this, Activity2.class), 2);
				
			}
		});
    }
}

二级页面
public class Activity1 extends Activity{
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
		   setContentView(R.layout.activity1);
		   
//		   findViewById(R)
		   Button button=(Button) findViewById(R.id.button11);
		   button.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
//			  Intent intent=new Intent(Activity1.this, StartResultTestActivity.class)	;
				Intent intent=new Intent();
			  setResult(1, intent);
			  finish();//如果不加这句话   这个页面将不会发生跳转到主界面      这句话的本质是:结束当前的 把它从activity的栈顶中移除  
			}
		});
		   
	 }
}






参考:http://www.cnblogs.com/linjiqin/archive/2011/06/03/2071956.html




你可能感兴趣的:(Android)