SoFunction
Updated on 2025-03-08

Android development tutorial: StartActivityForResult usage method

A small thing I recently encountered this situation. I modified some content from a page MainActivity and needed to jump to a new EditActivity to do the modification operation. After the modification is completed, I will return to the previous MainActivity. Because the information has been modified, it is best to re-get it from the server, so I need to re-get it in MainActivity. If the operation of re-get data is placed in the onStart() method of MainActivity, then MainActivity will jump to another place and return and does not need to request data. It will inevitably request it again, which will increase the pressure on the server in vain.

In other words, there is something that can tell where to jump back to MainActivity, so that you can determine whether a network request needs to be made again.

Here we will introduce the startActivityForResult() method.

startActivityForResult(Intent, int)

The first parameter Intent, just like the Intent in the ordinary startActivity(), it places the activity to be requested and the data that may be needed.

The second parameter int is a request code, an integer type. This can be defined by yourself, but this number must be greater than or equal to 0. Because MainActivity may jump to multiple pages, if some information needs to be returned after these pages are used, then there must be an identifier to indicate which page the information returned is.

The second method:

setResult(int),setResult(int, Intent)

The first parameter int is a return code, an integer, which is also defined by yourself to indicate what happens after the execution of this page, whether it is successful or failed, or something else. Anyway, if you return an integer number, you just need to know its meaning.

The second optional parameter is an Intent that can be used to store data.

The third method:

onActivityForResult(int, int, Intent)

This method is to call this after the requested Activity completes the task and is finished(), provided that the Activity you starts is started through startActivityForResult().

The first parameter int is the request code, which is the request code in startActivityForResult().

The second parameter int is the return code, which is the parameter set in the setResult() method.

The third parameter Intent is the Intent that places data in setResult(int, Intent).

For detailed use, please see the demo below.

First of all, there are two buttons in it, which jump to two activities respectively.

Copy the codeThe code is as follows:

public class MainActivity extends Activity {

    public static final int REQUEST_A = 1;
    public static final int REQUEST_B = 2;

    private Button btnA = null;
    private Button btnB = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        (savedInstanceState);
        setContentView(.activity_main);

//findViewById()

//Button A listener
        (new OnClickListener() {

            @Override
            public void onClick(View arg0) {

//Skip to ActivityA page
                Intent i = new Intent(, );

//Send request code REQUEST_A
                startActivityForResult(i, REQUEST_A);
            }
        });

//B button listener
        (new OnClickListener() {

            @Override
            public void onClick(View v) {

//Skip to ActivityB page
                Intent i = new Intent(, );

//Send request code REQUEST_B
                startActivityForResult(i, REQUEST_B);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

//First determine which page is returned
        switch (requestCode) {
        case REQUEST_A:

//Judge the situation back, whether it is successful, failure or something else...
            switch (resultCode) {
            case ActivityA.RESULT_SUCCESS:
//Successful
                break;
            case ActivityA.RESULT_FAILED:
//Failed
                break;
            }
            break;
        case REQUEST_B:
//Same as above…
            break;
        }
    }
}

Next is to deal with something specifically in this category. Then, it will fail successfully and return the result to MainActivity.

Copy the codeThe code is as follows:

public class ActivityA extends Activity {

    public static final int RESULT_SUCCESS = 0;
    public static final int RESULT_FAILED = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        (savedInstanceState);
//Find various controls...

//I am doing something, modifying data, and all kinds of things.

//Assuming the data submission fails
        boolean result = false;

//Set the returned result, and the constants are defined above
        if (result) {
            setResult(RESULT_SUCCESS);
        } else {
            setResult(RESULT_FAILED);
        }

//End, return to MainActivity
        finish();
    }
}