StartActivityForResult - check for existence of intent extra or use custom result code?

mannykary

I have a search button in MainActivity which launches SearchActivity. In SearchActivity, the user can either choose from one of the predefined categories listed, or can enter in a search query. The SearchActivity will return a different extra depending on which way the user searches. The code that runs in MainActivity will depend on which extra is returned.

I'm debating which way is better or more "correct". I've coded it both ways, and it works either way.

The first way I coded it was to check for the existence of the intent extra:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == GET_SEARCH_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            if (data.getExtras().containsKey("extra1")) {
                extra1 = data.getStringExtra("extra1");
            }
            if (data.getExtras().containsKey("extra2")) {
                extra2 = data.getStringExtra("extra2");
            }
        }
        // plus rest of code for checking for RESULT_CANCELED
    }
}

The other way I coded it is by using custom result codes. The result codes RESULT_OK_CATEGORY and RESULT_OK_SEARCH are public static variables in MainActivity so that they can be accessed from SearchActivity, and sent back as a result code through setResult(MainActivity.RESULT_OK_CATEGORY, intent) or setResult(MainActivity.RESULT_OK_SEARCH, intent) respectively.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == GET_SEARCH_REQUEST_CODE) {
        if (resultCode == RESULT_OK_CATEGORY) {
            extra1 = data.getStringExtra("extra1");
        } else if (resultCode == RESULT_OK_SEARCH) {
            extra2 = data.getStringExtra("extra2");
        }
        // plus rest of code for checking for RESULT_CANCELED
    }
}

Which way is better, and why? Checking for the existence of the extra, or checking for a custom result code?

gknicker

You should use resultCode because it's completely under your control. You may lose values set in Intent's extras, as revealed in this question (referenced by Ajay in the comments).

Note, if you use many custom result codes, it has been recommended to use a switch statement for code clarity.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

StartActivityForResult - check for existence of intent extra or use custom result code?

From Dev

How to use intent startactivityforresult method?

From Dev

Android startActivityForResult returning result code on BackPressed event

From Dev

startActivityForResult(Intent intent, int requestCode, Bundle options) how to retrieve the extra bundle option?

From Dev

check existence of ftp file and parse exit code

From Dev

Unable to StartActivityForResult() with camera Intent

From Dev

Unable to StartActivityForResult() with camera Intent

From Dev

How to use Socket as Intent extra in Android?

From Dev

Result code for sharing intent for instagram android

From Dev

startActivityForResult: No Activity found to handle Intent

From Dev

How to pass an Intent Extra from a custom ListView Adapter?

From Dev

Caesar code in C extra letter for result

From Dev

Maven and code metrics: check for existence of a test case for each class

From Dev

Need to change this JQuery code to check for element existence prior to processing

From Dev

Is it ok to use SELECT * for doing an existence check in T-SQL

From Dev

Is it ok to use SELECT * for doing an existence check in T-SQL

From Dev

Custom RecyclerAdapter and startActivityForResult

From Dev

How to use intent in a custom dialog box button?

From Dev

How to use intent in a custom dialog box button?

From Dev

how to use intent and bundles for custom list view

From Dev

Result code code is -1 and returned intent is null in onActivityResult()

From Dev

Django. Check in template or use an extra field?

From Dev

How to use StartActivityForResult()

From Dev

Using Tabactivity Result code is getting 0,the return Intent data is null

From Dev

Laravel check for asset existence

From Dev

Check for class existence in Swift

From Dev

Check for existence of parameter in function

From Dev

check for multiple attributes existence

From Dev

Check existence of IndexedDB database

Related Related

  1. 1

    StartActivityForResult - check for existence of intent extra or use custom result code?

  2. 2

    How to use intent startactivityforresult method?

  3. 3

    Android startActivityForResult returning result code on BackPressed event

  4. 4

    startActivityForResult(Intent intent, int requestCode, Bundle options) how to retrieve the extra bundle option?

  5. 5

    check existence of ftp file and parse exit code

  6. 6

    Unable to StartActivityForResult() with camera Intent

  7. 7

    Unable to StartActivityForResult() with camera Intent

  8. 8

    How to use Socket as Intent extra in Android?

  9. 9

    Result code for sharing intent for instagram android

  10. 10

    startActivityForResult: No Activity found to handle Intent

  11. 11

    How to pass an Intent Extra from a custom ListView Adapter?

  12. 12

    Caesar code in C extra letter for result

  13. 13

    Maven and code metrics: check for existence of a test case for each class

  14. 14

    Need to change this JQuery code to check for element existence prior to processing

  15. 15

    Is it ok to use SELECT * for doing an existence check in T-SQL

  16. 16

    Is it ok to use SELECT * for doing an existence check in T-SQL

  17. 17

    Custom RecyclerAdapter and startActivityForResult

  18. 18

    How to use intent in a custom dialog box button?

  19. 19

    How to use intent in a custom dialog box button?

  20. 20

    how to use intent and bundles for custom list view

  21. 21

    Result code code is -1 and returned intent is null in onActivityResult()

  22. 22

    Django. Check in template or use an extra field?

  23. 23

    How to use StartActivityForResult()

  24. 24

    Using Tabactivity Result code is getting 0,the return Intent data is null

  25. 25

    Laravel check for asset existence

  26. 26

    Check for class existence in Swift

  27. 27

    Check for existence of parameter in function

  28. 28

    check for multiple attributes existence

  29. 29

    Check existence of IndexedDB database

HotTag

Archive