首页 > 解决方案 > 如何通过意图提供数据重新加载相同的活动?

问题描述

以下是我的代码,但在重新加载活动后我没有得到restaurant_id

Intent refresh = new Intent(getApplicationContext(), RestaurantDetailActivity.class);
refresh.putExtra("restaurant_id", mDataset.get(position).getRestaurantId());
startActivity(refresh);
finish();

标签: androidandroid-studioandroid-intent

解决方案


请尝试以下步骤...

  1. 刷新RestaurantDetailActivity

     Intent intent = new Intent(RestaurantDetailActivity.this, RestaurantDetailActivity.class);
     Bundle bundle = new Bundle();
     bundle.putInt("restaurant_id", mDataset.get(position).getRestaurantId());
     intent.putExtras(bundle);
     startActivity(intent);
     RestaurantDetailActivity.this.finish();
    
  2. OnCreate()你应该在同一个活动中这样做

     if(getIntent().getExtras() != null) {
         Bundle extras = getIntent().getExtras();
         int restaurantId = extras.getInt("restaurant_id");
         // todo with restaurantId
     }
    

推荐阅读