Android开发:如何将Restaurant页面生成的ID传递至非直接后续页面?
Hey there! Let's break down and solve your two Android development issues step by step.
First, let's figure out why the correct Restaurant ID isn't reaching your Table page, and instead you're getting the node name 'restaurantID' instead of the unique ID. Here are the key checks to make:
Verify your Intent code for TableActivity matches the working MenuActivity code
Make sure you're using the same constantRESTAURANT_ID(instead of hardcoding the string "restaurantID") when putting the extra. Your code should look identical to the working Menu transfer code:Intent intent = new Intent(getApplicationContext(), TableActivity.class); intent.putExtra(RESTAURANT_ID, restaurant.getRestaurantID()); // Use the same constant here! startActivity(intent);Check how you're receiving the ID in TableActivity
Ensure you're using the sameRESTAURANT_IDconstant to retrieve the value, not the string "restaurantID". For example:// Adjust the type (String/int) based on your Restaurant ID's data type String restaurantId = getIntent().getStringExtra(RESTAURANT_ID);Confirm the source value is valid
Before passing the ID, log its value to make surerestaurant.getRestaurantID()is returning the actual unique ID, not the string "restaurantID":Log.d("DebugTag", "Restaurant ID to pass: " + restaurant.getRestaurantID());If the log shows "restaurantID" instead of the unique ID, the issue is with how you're generating/storing the ID in your
restaurantobject, not the Intent transfer.
If you need to send the Restaurant ID to a page that's not an immediate next step (like directly from Restaurant to Table, skipping Menu, or accessing it later in any page), here are reliable methods to use:
SharedPreferences (for persistent, cross-page access)
Store the ID in SharedPreferences once from the Restaurant page, then retrieve it from any other page:// Save from RestaurantActivity SharedPreferences prefs = getSharedPreferences("MyAppPreferences", MODE_PRIVATE); prefs.edit().putString(RESTAURANT_ID, restaurant.getRestaurantID()).apply(); // Retrieve from TableActivity (or any other page) SharedPreferences prefs = getSharedPreferences("MyAppPreferences", MODE_PRIVATE); String restaurantId = prefs.getString(RESTAURANT_ID, null); // null is the default if not foundSingleton Data Holder Class
Create a global singleton to hold the ID in memory, accessible from any activity:// Create this singleton class in your project public class AppSession { private static AppSession instance; private String currentRestaurantId; private AppSession() {} // Private constructor to enforce singleton public static AppSession getInstance() { if (instance == null) { instance = new AppSession(); } return instance; } // Getter and setter for the ID public String getCurrentRestaurantId() { return currentRestaurantId; } public void setCurrentRestaurantId(String id) { this.currentRestaurantId = id; } } // Save from RestaurantActivity AppSession.getInstance().setCurrentRestaurantId(restaurant.getRestaurantID()); // Retrieve from TableActivity String restaurantId = AppSession.getInstance().getCurrentRestaurantId();Forward Through Intermediate Pages
If you're going through Menu to get to Table, just pass the ID along from Menu to Table once you receive it:// In MenuActivity, first receive the ID String restaurantId = getIntent().getStringExtra(RESTAURANT_ID); // Then pass it to TableActivity when navigating Intent tableIntent = new Intent(this, TableActivity.class); tableIntent.putExtra(RESTAURANT_ID, restaurantId); startActivity(tableIntent);Jetpack ViewModel (for lifecycle-aware sharing)
Use a shared ViewModel to hold the ID, which works great for sharing data between components without worrying about lifecycle changes:// Create a shared ViewModel class public class SharedRestaurantViewModel extends ViewModel { private MutableLiveData<String> restaurantId = new MutableLiveData<>(); public void setRestaurantId(String id) { restaurantId.setValue(id); } public LiveData<String> getRestaurantId() { return restaurantId; } } // Set the ID from RestaurantActivity SharedRestaurantViewModel viewModel = new ViewModelProvider(this).get(SharedRestaurantViewModel.class); viewModel.setRestaurantId(restaurant.getRestaurantID()); // Observe and retrieve from TableActivity SharedRestaurantViewModel viewModel = new ViewModelProvider(this).get(SharedRestaurantViewModel.class); viewModel.getRestaurantId().observe(this, restaurantId -> { // Use the ID here (this callback triggers when the value is set) });
内容的提问来源于stack exchange,提问作者Mayur Sadhu

