Android 如何重新加载相同的活动并有目的地提供数据?

Android 如何重新加载相同的活动并有目的地提供数据?,android,android-studio,android-intent,Android,Android Studio,Android Intent,下面是我的代码,但在重新加载活动后,我没有获得餐厅id Intent refresh = new Intent(getApplicationContext(), RestaurantDetailActivity.class); refresh.putExtra("restaurant_id", mDataset.get(position).getRestaurantId()); startActivity(refresh); finish(); 尝试以下步骤 刷新RestaurantDetai

下面是我的代码,但在重新加载活动后,我没有获得
餐厅id

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

尝试以下步骤

  • 刷新
    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();
    
  • OnCreate()
    中,您应该在相同的活动中这样做

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

  • 尝试以下步骤

  • 刷新
    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();
    
  • OnCreate()
    中,您应该在相同的活动中这样做

    if(getIntent().getExtras() != null) {
        Bundle extras = getIntent().getExtras();
        int restaurantId = extras.getInt("restaurant_id");
        // todo with restaurantId
    }
    
  • 快乐编码
  • 试试这个

    • 在同样的活动中使用意图

       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);
      
      private void loadData() {
      Bundle bundle = intent.getExtras();
      final String restaurantId = bundle.getString("restaurantId ");
      }
      
    • loadData()在同一活动中的onCreate方法中使用,如下所示

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

    • 您可以定义一个函数来加载活动的数据,并在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
          }
      
    我希望它有帮助

    试试这个

    • 在同样的活动中使用意图

       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);
      
      private void loadData() {
      Bundle bundle = intent.getExtras();
      final String restaurantId = bundle.getString("restaurantId ");
      }
      
    • loadData()在同一活动中的onCreate方法中使用,如下所示

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

    • 您可以定义一个函数来加载活动的数据,并在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
          }
      

    我希望它对您有所帮助

    当您重新加载活动时,您已经丢失了来自后台活动的数据。您这里所说的“重新加载”是什么意思?请解释您的问题。您可以尝试使用onResume并检查餐厅id是否有值…为什么不能尝试这意味着重新创建活动当您重新加载活动时,您已经丢失了来自后台活动的数据。您这里所说的“重新加载”是什么意思?请解释您的问题。您可以尝试使用onResume并检查餐厅id是否有价值…为什么您不能尝试这意味着重新创建活动