安卓开发:从Salad Menu Activity返回时回到Home的Menu Categories Fragment
Got it, let's walk through how to make sure pressing back from your Salad Menu Activity lands you directly on the Menu Categories Fragment in Home Activity. Here's a step-by-step implementation:
Step 1: Add Fragment Switching Logic in Home Activity
First, update your HomeActivity to support loading a specific fragment when requested. This includes handling both initial launch and when the activity is brought back to the foreground:
public class HomeActivity extends AppCompatActivity { // Constants for clean parameter handling public static final String EXTRA_TARGET_FRAGMENT = "target_fragment"; public static final String FRAGMENT_MENU_CATEGORIES = "menu_categories"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); // Check if we need to load a specific fragment on launch handleTargetFragmentIntent(getIntent()); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent); // Handle fragment request when HomeActivity is brought back to top handleTargetFragmentIntent(intent); } private void handleTargetFragmentIntent(Intent intent) { if (intent != null && intent.hasExtra(EXTRA_TARGET_FRAGMENT)) { String targetFragment = intent.getStringExtra(EXTRA_TARGET_FRAGMENT); if (FRAGMENT_MENU_CATEGORIES.equals(targetFragment)) { switchToMenuCategoriesFragment(); } } else { // Load your default fragment (the one shown when HomeActivity first opens) loadDefaultFragment(); } } private void switchToMenuCategoriesFragment() { // Avoid redundant fragment transactions Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container); if (!(currentFragment instanceof MenuCategoriesFragment)) { MenuCategoriesFragment fragment = new MenuCategoriesFragment(); getSupportFragmentManager() .beginTransaction() .replace(R.id.fragment_container, fragment) .commit(); } } private void loadDefaultFragment() { // Replace with your actual default fragment initialization // Example: // DefaultHomeFragment defaultFragment = new DefaultHomeFragment(); // getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, defaultFragment).commit(); } }
Step 2: Customize Back Press Behavior in Salad Menu Activity
Override the back press logic in SaladMenuActivity to explicitly send a request to Home Activity to load the Menu Categories Fragment:
public class SaladMenuActivity extends AppCompatActivity { @Override public void onBackPressed() { Intent intent = new Intent(this, HomeActivity.class); // Flags to reuse existing HomeActivity instance instead of creating a new one intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); // Pass the target fragment identifier intent.putExtra(HomeActivity.EXTRA_TARGET_FRAGMENT, HomeActivity.FRAGMENT_MENU_CATEGORIES); startActivity(intent); finish(); // Close the Salad Menu Activity } }
Step 3: Keep Existing Launch Logic Intact
Your code to launch Salad Menu Activity from MenuCategoriesFragment doesn't need any changes—just keep it as is:
// Inside MenuCategoriesFragment, where the "Salad menu" text is clicked saladMenuTextView.setOnClickListener(v -> { Intent intent = new Intent(getActivity(), SaladMenuActivity.class); startActivity(intent); });
How This Works
- When you press back from Salad Menu Activity, we send an intent to Home Activity with a parameter specifying which fragment to load.
- The
FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_SINGLE_TOPflags ensure Home Activity is brought to the foreground without being recreated, and itsonNewIntent()method is triggered. - The
handleTargetFragmentIntent()method checks the intent parameter and switches to the Menu Categories Fragment, avoiding unnecessary transactions if the fragment is already visible.
内容的提问来源于stack exchange,提问作者Prabhjeet Singh
相关产品推荐
相关产品推荐

