Activity之間數據交流(startActivityForResult , onActivityResult , setResult 的用法)

主要功能:android

在一個主界面(主Activity)上能鏈接往許多不一樣子功能模塊(子Activity上去),當子模塊的事情作完以後就回到主界面,或許還同時返回一些子模塊完成的數據交給主Activity處理。這樣的數據交流就要用到 主Activity 回調方法onActivityResult()。
 
下面介紹具體流程:
 
Activity中:
<1> Activity裏面傳遞一個Intent對象,給即將打開的 Activity,並用requestCode區分打開哪一個  Activity
startActivityForResult(Intent intent, int requestCode);

第一個參數Intent intent:  一個Intent對象app

第二個參數 int requestCode :>= 0, 主Activity結束時requestCode 將歸還在onActivityResult()中。以便肯定返回的數據是從哪一個Activity中返回
 
 
 
<2> Activity 裏重寫這個方法,接收 Activity 收傳入的 Intent的對象, Activity既能夠經過switch/ case語句用 requestCode 來區分不一樣的 Activity, 也能夠經過resultCode 來區分
onActivityResult(int requestCode, int resultCodeIntent data)
int requestCode: 這個整數requestCode提供給onActivityResult,是以便確認返回的數據是從哪一個Activity返回的。 這個requestCode     
                               startActivityForResult中的requestCode相對應。

int resultCode:    子Activity經過其setResult()方法返回。 ide

Intent data :          一個Intent對象,帶有返回的數據。
 
 
Activity中:
<3> Activity 調用這個方法把數據返回到 Activity ,同時也返回區分本身的resultCode
setResult(int resultCodeIntent data)

int resultCode:  子Activity結束時resultCode將歸還在主Activity的onActivityResult()中,通常爲RESULT_CANCELED , RESULT_OKthis

Intent data:       一個Intent對象,返回給父Activity的數據。spa

 
 
例子:
 
本實例一共有三個Activity。地球火星月球
 
點擊飛往火星跳轉到otherActivity,把地球帶給火星的消息顯示在otherActivity中。
點擊返回 地球時把火星帶給地球的消息傳到主Activity.

同理:.net

點擊飛往月球跳轉到thirdActivity,把地球帶給月球的消息顯示在thirdActivity中。
點擊返回地球時把月球帶給地球的消息傳到主Activity.
 地球--》飛往火星   火星 --》返回地球  

地球--》飛往月球 月球---》返回地球  對象

 

 

具體實現:blog

主Activity:ci

package  xiaosi.onActivityResult;
 
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  OnActivityResultActivity  extends  Activity
{
        private  Button                      button      =  null ;
        private  Button                      button1     =  null ;
        private  TextView              text  =  null ;
        private  static  final  int       Mars  = 0;
        private  static  final  int       Moon  = 1;
 
        @ Override
        public  void  onCreate(Bundle savedInstanceState)
      {
              super .onCreate(savedInstanceState);
            setContentView(R.layout.main);
            text = (TextView) findViewById(R.id.text);
            button = (Button) findViewById(R.id.button);
            button.setOnClickListener(  new  OnClickListener() {
                    @ Override
                    public  void  onClick(View v)
                  {
                        Intent intent =  new  Intent(OnActivityResultActivity. this  , otherActivity. class );
                        String content =  "地球來的消息:你好,我是來自地球上的小老鼠。我好想去大家的火星呀"  ;
                        intent.putExtra(  "FromEarth" , content);
                        startActivityForResult(intent, Mars);
                  }
            });
            button1 = (Button) findViewById(R.id.button1);
            button1.setOnClickListener(  new  OnClickListener() {
                    @ Override
                    public  void  onClick(View v)
                  {
                        Intent intent =  new  Intent(OnActivityResultActivity. this  , thirdActivity. class );
                        String content =  "地球來的消息:你好,我是來自地球上的小老鼠。我好想去大家月球"  ;
                        intent.putExtra(  "FromEarth" , content);
                        startActivityForResult(intent, Moon);
                  }
            });
      }
 
        @ Override  
        protected  void  onActivityResult( int  requestCode,  int  resultCode, Intent data)//重寫 onActivityResult方法
      {
              switch  (requestCode)
            {
              case  Mars:
                  Bundle MarsBuddle = data.getExtras();
                  String MarsMessage = MarsBuddle.getString(  "FromMars" );
                  text.setText(MarsMessage);
                    break ;
              case  Moon:
                  Bundle MoonBuddle = data.getExtras();
                  String MoonMessage = MoonBuddle.getString(  "FromMoon" );
                  text.setText(MoonMessage);
                    break ;
            }
      }
}
 
 

otherActivity:get

package  xiaosi.onActivityResult;
 
import  android.app.Activity;
import  android.content.Intent;
import  android.os.Bundle;
import  android.view.View;
import  android.widget.Button;
import  android.widget.TextView;
 
public  class  otherActivity  extends  Activity
{
        private  Button    button      =  null ;
 
        @ Override
        public  void  onCreate(Bundle savedInstanceState)
      {
              super .onCreate(savedInstanceState);
            setContentView(R.layout.other);
            Intent EarthIntent = getIntent();
            String EarthMessage = EarthIntent.getStringExtra( "FromEarth"  );
            button = (Button) findViewById(R.id.button);
            button.setOnClickListener(  new  View.OnClickListener() {
                    @ Override
                    public  void  onClick(View v)
                  {
                        Intent intent =  new  Intent(otherActivity. this , OnActivityResultActivity. class  );
                        String passString =  "火星來的消息:Hello,我是火星的Jack,很是高興你能來火星"  ;
                        intent.putExtra(  "FromMars" , passString);
                        setResult(RESULT_OK, intent);
                        finish();
                  }
            });
            TextView textView = (TextView) findViewById(R.id.othertext);
            textView.setText(EarthMessage);
      }
}
 
 

thirdActivity:

package  xiaosi.onActivityResult;
 
import  android.app.Activity;
import  android.content.Intent;
import  android.os.Bundle;
import  android.view.View;
import  android.widget.Button;
import  android.widget.TextView;
 
public  class  thirdActivity  extends  Activity
{
        private  Button    button      =  null ;
 
        @ Override
        public  void  onCreate(Bundle savedInstanceState)
      {
              super .onCreate(savedInstanceState);
            setContentView(R.layout.third);
            Intent EarthIntent = getIntent();
            String EarthMessage = EarthIntent.getStringExtra( "FromEarth"  );
            button = (Button) findViewById(R.id.button);
            button.setOnClickListener(  new  View.OnClickListener() {
                    @ Override
                    public  void  onClick(View v)
                  {
                        Intent intent =  new  Intent(thirdActivity. this , OnActivityResultActivity. class  );
                        String passString =  "月球來的消息:Hello,我是月球的Lucy,很是歡迎你來月球"  ;
                        intent.putExtra(  "FromMoon" , passString);
                        setResult(RESULT_OK, intent);
                        finish();
                  }
            });
            TextView textView = (TextView) findViewById(R.id.thirdtext);
            textView.setText(EarthMessage);
      }
}
相關文章
相關標籤/搜索