• activity通过onActivityResult间数据交互


    首先要创建2个activity 分别为MainActivity和OneActiivity

    MainActivity代码如下:

    package com.tp.soft.app;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class MainActivity extends Activity implements OnClickListener{
    
        private Button mTzBtn;
        private TextView mShow;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            mTzBtn = (Button) findViewById(R.id.tzBtn);
            mTzBtn.setOnClickListener(this);
            
            mShow = (TextView) findViewById(R.id.showMsg);
            
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if(requestCode == 1){
                String msg = data.getStringExtra("FromOneActivity");
                mShow.setText(msg);
            }
        }
    
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, OneActivity.class);
            String content = "Hello,OneActivity";
            intent.putExtra("FromMainActivity", content);
            startActivityForResult(intent, 1);
        }
    
    }

    其中调用

    startActivityForResult(intent, 1);中的1就是
    onActivityResult返回的requestCode。



    OneActivity代码如下:
    package com.tp.soft.app;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class OneActivity extends Activity implements OnClickListener {
        
        private Button mbackBtn;
        private TextView mShow;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_one);
    
            mbackBtn = (Button) findViewById(R.id.backBtn);
            mbackBtn.setOnClickListener(this);
            
            mShow = (TextView) findViewById(R.id.showTxt);
            Intent intent = getIntent();
            String show = intent.getStringExtra("FromMainActivity");
            mShow.setText(show);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.one, menu);
            return true;
        }
    
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(OneActivity.this, MainActivity.class);
            intent.putExtra("FromOneActivity", "Hello,MainActivity");
            setResult(RESULT_OK, intent);
            finish();
        }
    
    }
  • 相关阅读:
    Linux中常用操作命令(转)
    EntityFramework 优化建议(转)
    android 腾讯x5内核 浏览器
    Android MediaMetadataRetriever 读取多媒体文件信息,元数据(MetaData)
    Android MVP 利用rxjava 避免向Model传入监听方法
    Android 单元测试(junit、mockito、robolectric)
    Android 利用RecyclerView.Adapter刷新列表中的单个view问题
    android 5.0以上通知栏、状态栏图标变成白色
    Android 照相 滤镜
    Android 进程常驻(使用第三方MarsDaemon)(虽然不可用,但是还是保留下。)
  • 原文地址:https://www.cnblogs.com/tplovejava/p/3951787.html
Copyright © 2020-2023  润新知