Search code examples
androidfirebase-realtime-databaseandroid-activityandroid-lifecycle

(SOLVED)The Application Gives Error When Opened from Recent Applications


There is a SplashActivity in my application, the application is started with this activity. SplashActivity has a duration of 2.5 seconds, when this time is completed, HomeActivity opens. When the back button is pressed in the HomeActivity, the application is exited with finishAffinity(). Whenever I exit HomeActivity and then click on my application again from recent applications, the application cannot restart itself. I get user information in SplashActivity and give it to HomeActivty and I use Firebase for this.

The error it gives when the application cannot start itself is as follows:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.medita.mobile/com.medita.mobile.SplashActivity}: com.google.firebase.database.DatabaseException: Calls to setPersistenceEnabled() must be made before any other usage of FirebaseDatabase instance.

How can I fix this? I want the application to restart itself with SplashActivity when I exit HomeActivity by pressing the back button and click on the application again from the recent applications section.

I tried closing the activity by deleting it from the recent apps when closing it (finishAndRemoveTask()) but nothing worked. Therefore I think that everything ends with SplashActivity. Also I tried finishAffinity() in onStop() onPause() methods but it didn't work too.

My manifest file;

<activity
          android:name=".HomeActivity"
          android:exported="false"
          android:screenOrientation="portrait" />
<activity
          android:name=".SplashActivity"
          android:exported="true"
          android:screenOrientation="portrait" >
          <intent-filter>
              <action android:name="android.intent.action.MAIN" />
              <category android:name="android.intent.category.LAUNCHER" />
          </intent-filter>
</activity>

SplashActivity;

public class SplashActivity extends AppCompatActivity {

    private FirebaseAuth mAuth = FirebaseAuth.getInstance();
    private FirebaseUser currentUser;

    private DatabaseReference currentUserRef;
    private DatabaseReference userRef;
    String username = null;
    String email = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_screen);
        
        FirebaseApp.initializeApp(this);
        FirebaseDatabase.getInstance().setPersistenceEnabled(true);

        ...

        new Handler().postDelayed(new Runnable(){
            @Override
            public void run() {
                if (currentUser != null) {
                    getLoggedInUsername();
                    Intent home = new Intent(SplashActivity.this, HomeActivity.class);
                    startActivity(home);
                    overridePendingTransition(0,R.anim.fade_out);
                    finish();
                } else {
                    Intent mainIntent = new Intent(SplashActivity.this,LoginActivity.class);
                    SplashActivity.this.startActivity(mainIntent);
                    SplashActivity.this.finish();
                    overridePendingTransition(0,R.anim.fade_out);
                }

            }
        }, 2500);

    }

   private String getLoggedInUsername() {
        FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();

        ...

        return username;
    }
}

HomeActivtiy;

@Override
public void onBackPressed() {
    finishAffinity();
}

This is how I solved my problem;

I added this code to my SplashActivity.

static {
FirebaseDatabase.getInstance().setPersistenceEnabled(true); }

Solution

  • Create a custom Application class (a class that extends Application). In the onCreate() of your custom Application class, perform your Firebase initialization and configuration. This method gets called only once, when your application is started, before any activities are instantiated.

    Remove the Firebase initialization from your Activity.

    Add your custom Application class to your manifest in the <application> declaration like this:

    android:name="my.package.MyCustomApplication" (or whatever you call your custom Application class)
    

    Note: You can also remove the code in HomeActivity.onBackPressed(), as the default implementation of onBackPressed() will finish the current Activity and you only have one Activity active at this time.