zoukankan      html  css  js  c++  java
  • StartActivityForResult

    If want to get the data from the son activity, we can use the startactivityForResult.

    So For getting the data from the son activity, we just need to override the methods " onActivityResult" 。

    Let‘s code for this.

    At first, we should create a mather activity.

    package com.novasoftware;
    
    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.Button;
    
    public class MatherActivity extends Activity {
    
        private static String TAG="Mather Activity";
        
        private Button btnOpen;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
             setContentView(R.layout.main);
             LocalizeContols();
             RegisterEvents();     
        }
        
        private void LocalizeContols()
        {
            btnOpen=(Button)this.findViewById(R.id.btn_add_qrcode);
        }
        
        private void RegisterEvents()
        {
            //Add the click listener for the button view.
            btnOpen.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MatherActivity.this,SonActivity.class);
                    startActivityForResult(intent,1);
                }
            });
        }
        
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            String result = data.getExtras().getString("result");
            Log.i(TAG, result);
        }
        
    }

    when the onActivityResult methods, we can get the result with getextras. So we should add the data for the extras before we close the son activity.

    package com.novasoftware;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    public class SonActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.camera);
    
            Button btnClose = (Button) findViewById(R.id.btn_cancel_scan);
            btnClose.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.putExtra("result", "This is the son activity sends!");
                    SonActivity.this.setResult(RESULT_OK, intent);
                    SonActivity.this.finish();
                }
            });
    
        }
    
    }

    after we close the son activity, we will find there is a log in the LogCat: Mather Activity --- This is the son activity sends!.

  • 相关阅读:
    C#操作XML
    Eval调用函数
    SQL 日期时间函数
    vue中created和mounted区别
    记录uniapp的APP端分享到微信好友,链接为小程序页面,分享失败的BUG
    【News】Windows CE会死吗?答,死不了,只是变身了。
    【原创】工作总结
    【原创】工作总结之二
    【资源收集】关于WINCE网卡开发的知识收集
    【news】wince 7 preview release。大家可以去看看
  • 原文地址:https://www.cnblogs.com/xieshouzhu/p/2994487.html
Copyright © 2011-2022 走看看