Android Studio电商WebView应用:首页"Press again to exit" Toast异常求助
I’ve run into this exact issue before—your logic for detecting the homepage and handling back presses was reversed, which is why you’re seeing the opposite behavior. Let’s fix this with a clean, reliable implementation:
Step 1: Declare Required Variables
First, add these variables to your Activity class to track back press timing and your WebView instance:
private WebView mWebView; private long backPressedTimestamp = 0; // Set the double-press interval (2 seconds is standard) private static final long EXIT_DELAY = 2000;
Step 2: Initialize WebView
In your onCreate method, set up your WebView and load your homepage URL:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mWebView = findViewById(R.id.webView); WebSettings webSettings = mWebView.getSettings(); webSettings.setJavaScriptEnabled(true); // Enable JS if your app needs it // Load your ecommerce homepage URL mWebView.loadUrl("https://your-ecommerce-homepage.com"); }
Step 3: Override onBackPressed() with Correct Logic
This is where the magic happens. We’ll first check if the WebView can navigate back (meaning we’re not on the homepage). If it can, we just go back a page. If it can’t, we handle the double-press exit:
@Override public void onBackPressed() { // Case 1: We're not on the homepage (WebView can go back) if (mWebView.canGoBack()) { mWebView.goBack(); return; // Skip the exit logic entirely } // Case 2: We're on the homepage—handle double-press exit long currentTime = System.currentTimeMillis(); if (currentTime - backPressedTimestamp > EXIT_DELAY) { Toast.makeText(this, "Press again to exit", Toast.LENGTH_SHORT).show(); backPressedTimestamp = currentTime; } else { // Double press detected—exit the app super.onBackPressed(); } }
Optional: Add URL Check for Edge Cases
If your homepage redirects to another URL (common in ecommerce sites), add an extra check to confirm we’re actually on the homepage domain/path:
@Override public void onBackPressed() { String homepageUrl = "https://your-ecommerce-homepage.com"; boolean isOnHomepage = mWebView.getUrl() != null && (mWebView.getUrl().equals(homepageUrl) || mWebView.getUrl().startsWith(homepageUrl + "/")); if (mWebView.canGoBack() || !isOnHomepage) { mWebView.goBack(); return; } // Same double-press exit logic as before long currentTime = System.currentTimeMillis(); if (currentTime - backPressedTimestamp > EXIT_DELAY) { Toast.makeText(this, "Press again to exit", Toast.LENGTH_SHORT).show(); backPressedTimestamp = currentTime; } else { super.onBackPressed(); } }
Why This Works
- We prioritize WebView navigation first: If the user is on any page other than the homepage, pressing back just navigates them back in the WebView history.
- The double-press Toast only triggers when the WebView can’t go back (or we confirm we’re on the homepage URL), which ensures it only shows on your app’s starting page.
- We avoid the "Toast after exit" bug by only calling
super.onBackPressed()when we’re ready to exit—no Toast is shown after the app closes.
内容的提问来源于stack exchange,提问作者Ashok

