startActivityForResult 和 onActivityResult

Posted yuguangyuan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了startActivityForResult 和 onActivityResult相关的知识,希望对你有一定的参考价值。

参考:https://blog.csdn.net/hellobenji/article/details/79978701

https://www.jianshu.com/p/5f3953bc012e

在开发项目过程中,startActivityForResult是最常见的,它的使用场景就是:比如从AActivity跳转到BActivity,然后在BActivity中做一系列操作,然后在BActivity关闭时候需要把一些数据再回传给AActivity,或者当BActivity关闭后,需要让AActivity的界面或者数据发生一些变化,这个时候就需要用到 startActivityForResult。
MainActivity.java
package com.example.myapplication;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

    private static final String TAG = "test";
    final int REQUEST_CODE = 100;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate: test 1 ");
        Log.e(TAG, "onCreate: test 2 ");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button bt = findViewById(R.id.button1);
        bt.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this,"button is clicked",Toast.LENGTH_SHORT).show();
                Log.e(TAG,"==========匿名内部类======1====");
                Log.d(TAG,"==========匿名内部类======2====");
            }
        });

        Button bt2 = findViewById(R.id.button2);
        MyClickListener mcl = new MyClickListener();
        bt2.setOnClickListener(mcl);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.e(TAG, "onActivityResult: requestCode:" + requestCode);
        Log.e(TAG, "onActivityResult: resultCode:" + resultCode);
        if(requestCode == this.REQUEST_CODE){
            switch(resultCode){
                case RESULT_OK:
                    Bundle b = data.getExtras();
                    String str = b.getString("Result"); //获取 Result 中的值,为 "from Activity2"
                    Log.e(TAG, "onActivityResult: str:" + str);
                    break;
                default:
                    break;

            }

        }

    }

    class MyClickListener implements View.OnClickListener{

        @Override
        public void onClick(View view) {
            //在控制台输出一条语句
            Log.e(TAG,"刚刚点击的按钮时注册了内部类监听器对象的按钮");
            Intent intent = new Intent(MainActivity.this, secondActivity.class);
            //利用 Intent 传递数据。
            intent.putExtra("age",18);
            intent.putExtra("name","kelly");
            intent.putExtra("level",‘A‘);
            //利用 Bundle 传递数据。
            Bundle myBundle = new Bundle();
            myBundle.putString("author","mole");
            intent.putExtras(myBundle);
            //startActivity(intent);
            startActivityForResult(intent,REQUEST_CODE);
        }

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }
}

secondActivity.java

package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class secondActivity extends AppCompatActivity {
    private static final String TAG = "secondActivity";
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        
        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
        int age = intent.getIntExtra("age",100);
        char level = intent.getCharExtra("level",‘C‘);

        Log.e(TAG, "onCreate: name:" + name);
        Log.e(TAG, "onCreate: age:" + age);
        Log.e(TAG, "onCreate: level:" + level);
        Log.e(TAG, "onCreate() called with: savedInstanceState = [" + savedInstanceState + "]");

        Bundle myBundle = intent.getExtras();
        String author = myBundle.getString("author");
        Log.e(TAG, "onCreate: author:" + author);

        Intent intent2 = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString("Result","from Activity2");
        intent2.putExtras(bundle);
        this.setResult(RESULT_OK,intent2); //通过 intent 将数据返回给 Activity1,RESULT_OK


        Button bt = findViewById(R.id.button_second);

        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(secondActivity.this,"button is clicked",Toast.LENGTH_SHORT).show();
                Log.e(TAG,"==========匿名内部类======secondActivity====");
                finish();// end the activity
               // Intent intent = new Intent(secondActivity.this, MainActivity.class);
               // startActivity(intent);
            }
        });
    }
}

技术图片

 

 

 

以上是关于startActivityForResult 和 onActivityResult的主要内容,如果未能解决你的问题,请参考以下文章

startActivityForResult() 和 startActivity() 的区别? [关闭]

Android基础:startActivityForResult 和 onActivityResult 问题

startActivityForResult 和 onActivityResult

startActivityForResult(...) 立即返回 0

startActivityForResult的用法和demo

Android startActivityForResult和onActivityResult的基本用法