Android端Firestore读写无回调(onComplete等)问题求助
Hey there, let’s dig into this frustrating Firestore callback issue that’s blocking your app launch—nothing’s worse than an infinite loader that kills user experience, right? Based on the details you shared (only happening after the app runs in the foreground for minutes then opening another screen), here are targeted fixes to try:
Starting from Android 8.0 (API 26), the OS gets aggressive about limiting background resources to save battery. If your app runs in the foreground for a while, then switches to a new screen, the system might throttle network access for the old session, leaving Firestore requests hanging without triggering callbacks.
- Fix:
- Navigate to your device’s Settings > Apps > [Your App] > Battery > Disable "Battery Optimization".
- For apps that need persistent network access, you can request the
REQUEST_IGNORE_BATTERY_OPTIMIZATIONSpermission (note: Google Play has guidelines for this, only use it if strictly necessary). - Ensure you’re using a Foreground Service with a visible notification if your app needs to perform long-running operations—this prevents the system from killing your app’s process.
Older Firestore SDK versions had known bugs with callback delivery, especially when dealing with app lifecycle transitions. Using the Firebase BOM (Bill of Materials) makes it easy to keep all Firebase libraries in sync without manual version management.
- Fix:
In your app-levelbuild.gradle(Module level):// Use Firebase BOM to manage SDK versions implementation platform('com.google.firebase:firebase-bom:32.7.0') // Add Firestore dependency (no version needed with BOM) implementation 'com.google.firebase:firebase-firestore-ktx' // For Kotlin // OR for Java: // implementation 'com.google.firebase:firebase-firestore'
By default, Firestore uses relatively short timeouts for network requests. If your app is on a spotty network, requests might time out silently without triggering failure callbacks. Also, enabling offline persistence ensures operations queue up when network is lost and fire when connectivity returns.
- Fix:
Add this configuration when initializing Firestore (do this once, e.g., in your Application class):// Java FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder() .setConnectTimeout(60, TimeUnit.SECONDS) // Extend connect timeout .setReadTimeout(60, TimeUnit.SECONDS) // Extend read timeout .setPersistenceEnabled(true) // Enable offline persistence .build(); FirebaseFirestore.getInstance().setFirestoreSettings(settings);// Kotlin val settings = FirebaseFirestoreSettings.Builder() .setConnectTimeout(60, TimeUnit.SECONDS) .setReadTimeout(60, TimeUnit.SECONDS) .setPersistenceEnabled(true) .build() FirebaseFirestore.getInstance().firestoreSettings = settings
If you’re holding strong references to destroyed Activities/Fragments in your Firestore callbacks, the system might prevent the callback from executing (or it might execute but fail silently because the component is gone). This is common when opening a new screen and leaving the old component in a destroyed state.
- Fix:
- Use
WeakReferencefor Activity/Fragment references in callbacks to avoid memory leaks. - Cancel pending Firestore tasks when your component is destroyed. For example, in your Activity’s
onDestroy():// Store the Firestore task as a member variable private Task<Void> firestoreTask; @Override protected void onDestroy() { super.onDestroy(); if (firestoreTask != null && !firestoreTask.isComplete()) { firestoreTask.cancel(true); // Cancel the pending task } }
- Use
To get to the root cause, enable Firestore’s debug logging to see exactly what’s happening with your requests. You’ll be able to spot if requests are failing, timing out, or never reaching the server.
- Fix:
Add this line early in your app’s initialization (e.g., inApplication.onCreate()):
Then check Logcat for tags starting withFirebaseFirestore.setLoggingEnabled(true);Firestore,FirebaseAuth, orFirebaseNetwork—look for errors like "Request timed out" or "Network unavailable".
Start with updating the SDK and enabling logs first—those are usually the quickest ways to identify the issue. If the problem persists, check battery optimizations and lifecycle management.
内容的提问来源于stack exchange,提问作者Kelvin murithi

