Passing a Bundle on startActivity()?

后端 未结 6 1220
攒了一身酷
攒了一身酷 2020-11-22 03:28

What\'s the correct way to pass a bundle to the activity that is being launched from the current one? Shared properties?

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 04:18

    Passing data from one Activity to Activity in android

    An intent contains the action and optionally additional data. The data can be passed to other activity using intent putExtra() method. Data is passed as extras and are key/value pairs. The key is always a String. As value you can use the primitive data types int, float, chars, etc. We can also pass Parceable and Serializable objects from one activity to other.

    Intent intent = new Intent(context, YourActivity.class);
    intent.putExtra(KEY, );
    startActivity(intent);
    

    Retrieving bundle data from android activity

    You can retrieve the information using getData() methods on the Intent object. The Intent object can be retrieved via the getIntent() method.

     Intent intent = getIntent();
      if (null != intent) { //Null Checking
        String StrData= intent.getStringExtra(KEY);
        int NoOfData = intent.getIntExtra(KEY, defaultValue);
        boolean booleanData = intent.getBooleanExtra(KEY, defaultValue);
        char charData = intent.getCharExtra(KEY, defaultValue); 
      }
    

提交回复
热议问题