[Android keywords] use of startActivityForResult/onActivityResult/setResult method

Recently, I am writing an Android program. In the program, I need to use the Intent operation keyword startActivityForResult. Related to this keyword are onActivityResult and setResult. Here is a summary of its usage.

The form of the three in the API

//startActivityForResult与startActivity类似,只不过该方法可将方法打开新的Activity,新的Activity 关闭后会向前面的Activity传回数据,为了得到传回的数据,必须在前面的Activity中重写onActivityResult(int requestCode, int resultCode, Intent data)方法。
startActivityForResult(Intent intent, int requestCode)
onActivityResult(int requestCode, int resultCode, Intent data)
setResult(int resultCode, Intent data)

The role of the combination of the three

Through the situation in the API, the operation logic of the three can basically be deduced: suppose there are two activities A and B now, if we jump from A to B [startActivityForResult implementation], then B finishes the corresponding work and then finishes off B, and then Pass data to A [setResult implementation], and A performs corresponding operations after receiving the data [onActivityResult implementation]. Its operation logic can be represented by the following code:

A//启动B
Intent intent = new Intent();
intent.setClass(A.this, B.class);
//1是我们自己定义常量,用于标注由A->B这个活动,对应下面使用到的onActivityResult中的requestCode
startActivityForResult(intent, 1);
B//do something
//此方法中的 1 表示onActivityResult的resultCode,一般也用 RESULT_OK(1)和RESULT_CANCELED(0)表示,data一般为 Intent类型的数据,也可以为 null
setResult(1, data);
finish();

Then activity B returns to A to execute the onActivityResult method.

code example

Code in ActivityA.Java:

button1.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View view) {
    
    
                Intent intent = new Intent("com.example.activitytest.ACTTON_START");
                startActivityForResult(intent,1);//打开活动B
            }
        });

ActivityB.java code

In activity B, use the setResult method to set the result to be returned. setResult() receives two parameters. The first one is the result returned by the activity. Generally, RESULT_OK and RESULT_CANCELED are used, and the other is to pass back the Intent with data .

button2.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View view) {
    
    
                Intent intent = new Intent();
                intent.putExtra("data_return","测试一下");//要返回的结果
                setResult(RESULT_OK,intent);
                finish();//销毁活动B
            }
        });

After activity B is destroyed, the onActivityResult() method of the previous activity (that is, activity A) will be called back (if you want to get the passed result in activity A, you need to override this method).

@Override   //                        请求码          处理结果          带返回数据的Intent                  
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    
    
    if(data!=null){
    
    
        switch (requestCode){
    
    
            case 1 :
                if(resultCode == RESULT_OK){
    
    
                    String returnData = data.getStringExtra("data_return");
                    Log.d("MainActivity",returnData);
                }
                break;
                default:
        }
      }else{
    
    
      return ;
    }

In this way, after activity B is destroyed, it will return to activity A and get the data returned by activity B. But if the user does not define the button button2 in Activity B, but presses the Back button, then override the onBackPressed() method in ActivityB.java.

            @Override
            public void onBackPressed() {
    
    
                Intent intent = new Intent();
                intent.putExtra("data_return","测试一下");
                setResult(RESULT_OK,intent);
                finish();
            }
        });

It should be noted that when returning to ActivityA.java, the relationship between data and null needs to be judged in onActivityResult, which is conducive to the robustness of the program.

Guess you like

Origin blog.csdn.net/qq_29750461/article/details/131744925