【问题标题】:How to reload same activity with giving data with intent?如何通过意图提供数据重新加载相同的活动?
【发布时间】:2018-11-19 04:07:57
【问题描述】:

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

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

【问题讨论】:

  • 当您重新加载 Activity 时,您丢失了来自后面 Activity 的数据。
  • 这里的RELOAD 是什么意思?请解释您的问题。
  • 您可以尝试使用 onResume 并检查餐厅 id 是否有价值...
  • 你为什么不能试试这个stuff.mit.edu/afs/sipb/project/android/docs/training/basics/…意味着重新创建活动

标签: android android-studio android-intent


【解决方案1】:

尝试以下步骤...

  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
     }
    

【讨论】:

    【解决方案2】:

    试试这个

    • 像这样在您的同一活动中使用意图

         Intent intent = new Intent(MainActivity.this, MainActivity.class);
                       Bundle bundle = new Bundle();
                       bundle.putExtra("restaurant_id", 
                       list.get(position).getRestaurantId());
                       intent.putExtras(bundle);
                       startActivity(intent);
      
    • loadData() 在您的 onCreate 方法中使用类似这样的相同活动

        private void loadData() {
        Bundle bundle = intent.getExtras();
        final String restaurantId = bundle.getString("restaurantId ");
        }
      
    • 您可以在活动中使用 OnResume 方法

    • 您可以定义一个函数来为 Activity 加载数据并在 onResume() 中调用它。

    • loadData方法调用onCreate方法和onResume方法

           @Override
           public void onResume() {
           super.onResume();
      
            loadData();
      
           if (restaurant_id.equals(null)){
      
                  //write your code here
             }else{
      
           //write your code here
            }
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-10
      相关资源
      最近更新 更多