Will someone please explain RESULT_FIRST_USER

前端 未结 4 1022
甜味超标
甜味超标 2020-12-29 19:14

I don\'t understand the meaning, value, or importance of RESULT_FIRST_USER, other than that my own result codes must be greater than 1. Will someone please expl

相关标签:
4条回答
  • 2020-12-29 19:58

    An activity result is a 32-bit integer. The possible values are divided into three ranges:

    • -2 and below: Unused.
    • -1 and 0: System-defined values. That is, activity results defined by Android.
    • 1 and above: User-defined values. That is, activity results defined by app developers.

    RESULT_FIRST_USER identifies the first value in the user-defined range. The following sample definitions show how system- and user-defined values fit together:

    public static final int RESULT_OK = -1; // Defined by Android. You don't write this code.
    public static final int RESULT_CANCELED = 0; // Defined by Android. You don't write this code.
    public static final int RESULT_ENDED_GAME = RESULT_FIRST_USER + 0; // Defined by an app.
    public static final int RESULT_ACTIVATED_RADAR = RESULT_FIRST_USER + 1; // Defined by an app.
    public static final int RESULT_LAUNCHED_ROCKETS = RESULT_FIRST_USER + 2; // Defined by an app.
    
    0 讨论(0)
  • 2020-12-29 19:59

    When you finish an activity, you can call setResult(RESULT_CODE) to send back data to another activity. If you don't call this method, the default value will be RESULT_CANCELLED (which equals 0)

    Example of returning data:

    Intent intent= new Intent();
    intent.putExtra("data",data);
    setResult(YOUR_RESULT_CODE,intent);
    finish();
    
    0 讨论(0)
  • 2020-12-29 20:02

    The answer to the question is actually the combination of comments from @CommonsWare and @Chris. So, for the sake of progeny, I am going to consolidate the comments and make it available in one place.

    Basically, there are two predefined constants for the requestCode and they are Activity.RESULT_OK and Activity.RESULT_CANCELLED. However, android developers can also set custom codes for their apps by using the offset Activity.RESULT_FIRST_USER. Doing so ensures that there are no clashes between constants set at the OS level and the app level.

    Purely, my opinion, I think that the FIRST USER suffix is meant to refer to developers – just like how end consumer refers to consumers of a said product – who are the first users before the app users.

    Below is an example of how you can use this offset,

    public static final int MY_RESULT_CODE = Activity.RESULT_FIRST_USER + 1;
    
    0 讨论(0)
  • 2020-12-29 20:07

    When an activity ends, it can call setResult(int) to return data back to its parent.

    It must always supply a result code, which can be the standard results

    • RESULT_CANCELED (Standard activity result: Operation canceled. Constant Value: 0)
    • RESULT_OK (Standard activity result: operation succeeded. Constant Value: -1), or any custom values starting at RESULT_FIRST_USER (Start of user-defined activity results. Constant Value: 1). In addition, it can optionally return back an Intent containing any additional data it wants.

    So, bottom line since you must supply the result code Android "helps' you a bit by saying: please state if the result code of this Activity is OK, CANCELED or you have your own, custom made, result.

    0 讨论(0)
提交回复
热议问题