putExtra() in android

后端 未结 2 898
暗喜
暗喜 2020-12-30 07:22

i want to know the usage of putExtra from very basic level

相关标签:
2条回答
  • 2020-12-30 07:39

    Add extended data to the intent.

    The name must include a package prefix. For example, the app "com.android.contacts" would use names like "com.android.contacts.ShowAll".

    Parameters:

    name: The name of the extra data, with package prefix.

    value: The double array data value.

    Returns the same Intent object, for chaining multiple calls into a single statement.

    0 讨论(0)
  • 2020-12-30 07:53

    If you want add information to your intent you can use this method. This information is represented as tuple (key, value). There are the number of value types that can be included into the extras of intent (for instance, int, int[], Bundle, Parcelable, and so on). For each this method there is a corresponding "read" method that is used to get the information from the intent.

    So here is a possible example how to use this. Imagine that you want explicitly call the activity B from activity A and pass to it an array of integers:

    int intArray[] = {1,2,3,4};
    Intent in = new Intent(this, B.class);
    in.putExtra("my_array", intArray);
    startActivity(in);
    

    To read the information in activity B (in onCreate() method) you should use the following code:

    Bundle extras = getIntent().getExtras();
    int[] arrayInB = extras.getIntArray("my_array");
    
    0 讨论(0)
提交回复
热议问题