Passing a Bundle on startActivity()?

后端 未结 6 1243
攒了一身酷
攒了一身酷 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条回答
  •  攒了一身酷
    2020-11-22 04:11

    You can pass values from one activity to another activity using the Bundle. In your current activity, create a bundle and set the bundle for the particular value and pass that bundle to the intent.

    Intent intent = new Intent(this,NewActivity.class);
    Bundle bundle = new Bundle();
    bundle.putString(key,value);
    intent.putExtras(bundle);
    startActivity(intent);
    

    Now in your NewActivity, you can get this bundle and retrive your value.

    Bundle bundle = getArguments();
    String value = bundle.getString(key);
    

    You can also pass data through the intent. In your current activity, set intent like this,

    Intent intent = new Intent(this,NewActivity.class);
    intent.putExtra(key,value);
    startActivity(intent);
    

    Now in your NewActivity, you can get that value from intent like this,

    String value = getIntent().getExtras().getString(key);
    

提交回复
热议问题