How to use startActivityForResult in the android development tutorial

  • 2020-05-30 21:00:44
  • OfStack

Recently done a little thing encountered such a situation, I from 1 page MainActivity modify 1 some content, need to jump to a new EditActivity do modification operations, after the completion of the modification is to go back to the previous MainActivity, because information is changed, still had better from the server to obtain 1, then retrieve in MainActivity, if put to get the data of operation MainActivity onStart () method, the rest of the MainActivity jump to come back do not need to request data, it also will inevitably request once again, this white increased the pressure of the server.

In other words, you have to have one thing that can tell you where you're jumping back to MainActivity, so you can tell you whether you need to make a network request again.

Here's the startActivityForResult() method.

startActivityForResult(Intent, int)

The first parameter, Intent, is similar to Intent1 in normal startActivity(), with the requested Activity and possible data.

The second parameter, int, is a request code, integer, you can define this by yourself, but this number has to be greater than or equal to 0. Because MainActivity can jump to multiple pages, if they all need to return some information after using them, an identifier must be used to indicate which page was returned.

The second method:

setResult (int), setResult (int Intent)

The first parameter, int, is a return code, integer, and this is also a random definition of what happens after the page is executed, whether it succeeds or fails, or whatever, but it returns an integer, and you know what it means.

The second optional parameter is 1 Intent, which can be used to store data.

The third method:

onActivityForResult(int, int, Intent)

This method is called after the requested Activity has completed the task by finish(), provided that you start the Activity via 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 in setResult(int, Intent) where the data is placed.

See demo below for details.

The first is the MainActivity.java file, which has two buttons to jump to two Activity.


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) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //findViewById() At such a time 

        //A Button listener 
        btnA.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                // Jump to ActivityA page 
                Intent i = new Intent(MainActivity.this, ActivityA.class);

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

        //B Button listener 
        btnB.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                // Jump to ActivityB page 
                Intent i = new Intent(MainActivity.this, ActivityB.class);

                // 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:

            // Then judge the return, whether it is success or failure or something else... 
            switch (resultCode) {
            case ActivityA.RESULT_SUCCESS:
                // A success 
                break;
            case ActivityA.RESULT_FAILED:
                // failed 
                break;
            }
            break;
        case REQUEST_B:
            // Same as above... 
            break;
        }
    }
}

Next is ActivityA.java, which handles one thing in particular. And if you fail, you just return it back to MainActivity.


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) {
        super.onCreate(savedInstanceState);
        // Looking for controls of all kinds... 

        // I'm working on something, modifying the data, all sorts of things. 

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

        // Set the result to be returned. The constants are defined above 
        if (result) {
            setResult(RESULT_SUCCESS);
        } else {
            setResult(RESULT_FAILED);
        }

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


Related articles: