You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

安卓开发:从Salad Menu Activity返回时回到Home的Menu Categories Fragment

Solution to Navigate Back to Specific Fragment in Home Activity

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_TOP flags ensure Home Activity is brought to the foreground without being recreated, and its onNewIntent() 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:18:45