• onActivityResult()的用法


    在一个主界面(主Activity)上能连接往许多不同子功能模块(子Activity上去),当子模块的事情做完之后就回到主界面,或许还同时返回一些子模块完成的数据交给主Activity处理。这样的数据交流就要用到回调函数onActivityResult()。

    public class MainActivity extends Activity {
        public final int REQUESTCODE_FROM_MAIN_TO_OTHER = 1;
        public final int REQUESTCODE_FROM_MAIN_TO_PEPELU = 2;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button btn_start_other = (Button) findViewById(R.id.btn_start_other);
            btn_start_other.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.setClass(MainActivity.this, OtherActivity.class);
                    // 如果REQUESTCODE_MAIN> =0,当Activity结束时requestCode将归还在onActivityResult()中。
       //以便确定返回的数据是从哪个Activity中返回
    startActivityForResult(intent, REQUESTCODE_FROM_MAIN_TO_OTHER); } }); Button btn_start_pepelu = (Button) findViewById(R.id.btn_start_pepelu); btn_start_pepelu.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(MainActivity.this, PepeluActivity.class); startActivityForResult(intent, REQUESTCODE_FROM_MAIN_TO_PEPELU); } }); } /** * requestCode和startActivityForResult中的requestCode相对应 * resultCode和Intent是由子Activity通过其setResult()方法返回 */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case REQUESTCODE_FROM_MAIN_TO_OTHER: if (resultCode == Activity.RESULT_OK) { Toast.makeText(this, data.getStringExtra("name"), Toast.LENGTH_LONG).show(); Log.i("onactivityresultdemo", data.getStringExtra("name")); } else { Log.i("onactivityresultdemo", "i blabalabal"); } break; case REQUESTCODE_FROM_MAIN_TO_PEPELU: if (resultCode == Activity.RESULT_CANCELED) { Toast.makeText(this, data.getStringExtra("name"), Toast.LENGTH_LONG).show(); Log.i("onactivityresultdemo", data.getStringExtra("name")); } else { Log.i("onactivityresultdemo", "i blabalabal"); } break; default: break; } } }

     在OtherActivity中设置返回码和返回数据:

    public class OtherActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_other);
    
            Button btn = (Button) findViewById(R.id.btn_other);
            btn.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.putExtra("name", "cc");
                    // 设置返回码和返回携带的数据
                    setResult(Activity.RESULT_OK, intent);
    
                }
            });
        }
    
    }

    在PepeluActivity中设置返回码和返回数据:

    public class PepeluActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_pepelu);
            Button btn_pepelu=(Button) findViewById(R.id.btn_pepelu);
            btn_pepelu.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent();
                    intent.putExtra("name", "pepelu");
                    setResult(Activity.RESULT_CANCELED,intent);
                }
            });
        }
    }
  • 相关阅读:
    promise 理解
    强化学习的概念
    Ubuntu安装机器学习环境步骤
    jsp文件复制到web项目出错
    jdbc导致的问题
    C#窗体-猜数字
    软件工程结对作业01
    第二阶段冲刺10天 第3天进展报告
    第二阶段冲刺10天 第2天进展报告
    第二阶段冲刺10天 第1天进展报告
  • 原文地址:https://www.cnblogs.com/mada0/p/4819709.html
Copyright © 2020-2023  润新知