sending bundle to another activity

前端 未结 6 1044
再見小時候
再見小時候 2021-01-15 03:03

I am trying to send a bundle from one activity to another. When I load the bundle in the recieving activity all the information seems to be null. Here is some code:

相关标签:
6条回答
  • 2021-01-15 03:47
    Bundle b = new Bundle();
    b = toBundle(app.getHotelList().get(position));
    intent.putExtras(b);
    startActivity(intent);
    
    
    
    
    //In your next activity
    //Create another Bundle object and get the string by name
    
    Bundle bundle = getIntent().getExtras();
    if(bundle!=null){
     String hotelName = bundle.getString("hotelname");
    }
    
    0 讨论(0)
  • 2021-01-15 03:47

    Activity A (sending bundle): intent.putExtra("Hotel Bundle", b);

    Activity B (loading the bundle): getintent().getExtra("Hotel Bundle");

    not getintent().getExtra*s*

    0 讨论(0)
  • 2021-01-15 03:55

    You must use exact key sent:

    String hotelName = b.getString("hotelname");
    

    Updated!

    0 讨论(0)
  • 2021-01-15 03:58

    I don't know if anyone still needs this but, in your intent.putExtra("Hotel Bundle", b);

    It's either you send just intent.putExtra(b); so you can get the data via

    Bundle b = this.getIntent().getExtras();
    String hotelName = b.getString("hotelname");
    

    or if you want to keep the Hotel Bundle key

    Bundle b = this.getIntent().getBundleExtra("Hotel Bundle");
    String hotelName = b.getString("hotelname");
    

    Sorry, I needed the answer here, so might as well help those who run into this. Cheers.

    0 讨论(0)
  • 2021-01-15 03:59

    Use Intent.putExtra(Bundle) method.

     intent.putExtra(b);
    
    0 讨论(0)
  • 2021-01-15 04:00

    Instead of using your toBundle method, you should implement Parcelable, and write your Object to an Intent.

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