Home > Back-end >  How to make FirebaseApp don't initialize again when back to MainActivity?[Solved]
How to make FirebaseApp don't initialize again when back to MainActivity?[Solved]

Time:06-06

I am a beginner in learning Android studio and bad at English.

I have two Firebase projects in an android app and writing these code below.

     FirebaseOptions options1 = new FirebaseOptions.Builder()
                .setProjectId("fd-login-20e78")
                .setApplicationId("...")
                .setApiKey("...")
                .build();

        FirebaseOptions options2 = new FirebaseOptions.Builder()
                .setProjectId("fd-login1-dc1f7")
                .setApplicationId("...")
                .setApiKey("...")
                .build();

        FirebaseApp.initializeApp(this , options1, "first");
        FirebaseApp.initializeApp(this , options2, "secondary");

        FirebaseApp first = FirebaseApp.getInstance("first");
        FirebaseApp secondary = FirebaseApp.getInstance("secondary");

It work pretty well without any crash. But when I trying to go back to MainActivity from other Activities, it always crash and have this error :

Caused by: java.lang.IllegalStateException: FirebaseApp name first already exists!

I know the problem occurs because FirebaseApp is initialized again. How could I stop it when I back to MainAcitvity?

I had tried this way to solve the problem but it didn't work. The code below make App crash immediately. Errors shows that it doesn't initialize anything.

if (FirebaseApp.getApps(this).isEmpty()) {
    FirebaseApp.initializeApp(this , options1, "first");
    FirebaseApp.initializeApp(this , options2, "secondary");
}

Edit

public class MainActivity extends AppCompatActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {

    private TextView tv_register, tv_forget;
    private EditText ed_email1, ed_password1;
    private Button btn_login;
    private RadioGroup radioGroup;
    RadioButton rb1, rb2, rb3;
    private FirebaseAuth mAuth, mAuth2, mAuth3, login;
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;
    FirebaseApp first,secondary;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv_register = findViewById(R.id.tv_register2);
        tv_register.setOnClickListener(this);

        tv_forget = findViewById(R.id.tv_forget2);
        tv_forget.setOnClickListener(this);

        btn_login = findViewById(R.id.btn_login2);
        btn_login.setOnClickListener(this);

        ed_email1 = findViewById(R.id.ed_email2);
        ed_password1 = findViewById(R.id.ed_password2);

        rb1 = findViewById(R.id.rb1);
        rb2 = findViewById(R.id.rb2);
        rb3 = findViewById(R.id.rb3);
        radioGroup = findViewById(R.id.radioGroup);
        radioGroup.setOnCheckedChangeListener(this);

        FirebaseOptions options1 = new FirebaseOptions.Builder()
                    .setProjectId("fd-login-20e78")
                    .setApplicationId("...")
                    .setApiKey("...")
                    .build();

        FirebaseOptions options2 = new FirebaseOptions.Builder()
                    .setProjectId("fd-login1-dc1f7")
                    .setApplicationId("...")
                    .setApiKey("...")
                    .build();
            
            FirebaseApp.initializeApp(this , options1, "first");
            FirebaseApp.initializeApp(this , options2, "secondary");


        first = FirebaseApp.getInstance("first");
        secondary = FirebaseApp.getInstance("secondary");

        mAuth = FirebaseAuth.getInstance(first);
        mAuth2 = FirebaseAuth.getInstance(secondary);
    }

CodePudding user response:

If going back to MainActivity() using onBackPressed() is an option, meaning if the second activity is loaded from MainActivity() and you just want to go back, write the Firebase initialization logic in the onCreate() method of MainActivity() and use onBackPressed() in the second activity to go back to the MainActivity() like so:

Instead of the following:

val intent = Intent(this, MainActivity :: class.java)
startActivity(intent)

just write this:

onBackPressed()

If you are in a fragment use this:

activity?.onBackPressed()

This way, since you are using the onBackPressed() function and not an intent, a new instance of MainActivity() will not be created and the same instance which is available in the back stack will be loaded. As a result, onCreate() will not be called again. Instead, onResume() is called so the initialization logic will not run.

CodePudding user response:

You can create an Application class whose onCreate() method is only called at the start of the app and never after. For this, create a separate java class called MyApplication (or whatever name you like), extend it with Application class and in it's onCreate() method, execute your code like this:

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        FirebaseOptions options1 = new FirebaseOptions.Builder()
                .setProjectId("fd-login-20e78")
                .setApplicationId("...")
                .setApiKey("...")
                .build();

        FirebaseOptions options2 = new FirebaseOptions.Builder()
                .setProjectId("fd-login1-dc1f7")
                .setApplicationId("...")
                .setApiKey("...")
                .build();

        FirebaseApp.initializeApp(this , options1, "first");
        FirebaseApp.initializeApp(this , options2, "secondary");

        FirebaseApp first = FirebaseApp.getInstance("first");
        FirebaseApp secondary = FirebaseApp.getInstance("secondary");
    }
}

One more thing you have to do is to declare MyApplication name in AndroidManifest within application tag like this:

android:name=".MyApplication"

That's it. Let me know if something breaks.

  • Related