Search code examples
javaandroidandroid-activity

Launch-Activity onCreate() called again when start another Activity


My Android App launches with a simple Activity with shows a ProgressBar. Some data is initialized and then the MainActivity is loaded for the user to interact with.

The Problem:

When startActivity() in showMainActivity() is called, the onCreate() - Method in the InitActivity is called again. This causes the data to be initialized again and the MainActivity to be started twice. Strangely this does not end in an endless loop but happens only once.

Does anyone have an idea why startActivity() causes onCreate() of InitActivity to be called again?

Code:

public class InitActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {

//GETS CALLED AGAIN AFTER showMainActivity()

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_init);

    DatabaseInitTask databaseInitTask = new DatabaseInitTask(getApplicationContext(), new DatabaseInitTask.DatabaseInitCallback() {
        @Override
        public void onInitCompleted() {

                    showMainActivity();
            });
        }
    });

    databaseInitTask.execute("");
}

private void showMainActivity() {
    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(intent);
}
}

Manifest:

   <activity
        android:name=".Ui.MainActivity"
        android:label="@string/app_name"
        android:windowSoftInputMode="adjustPan">
        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
            <data
                android:host="mydomain.com"
                android:scheme="https"/>
        </intent-filter>
    </activity>
    <activity
        android:name=".Ui.InitActivity"
        android:label="@string/app_name"
        android:windowSoftInputMode="adjustPan">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Solution

  • I have found the solution in my case. I present my solution here in case someone has a similar problem.

    The problem was that I called the following function in MainActivity:

    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    

    This caused the complete activity stack (including InitActivity) to be recreated. savedInstanceState of InitActivity was null and I could not react to it.

    The faulty program flow was as follows:

    App starts with InitActivity -> Some data are initialized -> InitActivity starts MainAcitvity -> MainActivity calls setDefaultNightMode which results in InitActivity being restarted but savedInstanceState being null -> InitActivity starts MainAcitvity again

    The reason why this didn't end in an infinite loop is that MainAcitvity checked if the night mode is already set correctly and called setDefaultNightMode only if it was necessary to change the setting.

    How I solved it:

    I moved setDefaultNightMode to InitActivity. This still causes onCreate to be called a second time in InitActivity but this time savedInstanceState is not null and I can use savedInstanceState to prevent MainActivity from being started a second time.

    I don't fully understand why savedInstanceState is not null anymore but it works now.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_init);
    
        if(savedInstanceState == null) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        } else
        {
        DatabaseInitTask databaseInitTask = new DatabaseInitTask(getApplicationContext(), new DatabaseInitTask.DatabaseInitCallback() {
            @Override
            public void onInitCompleted() {
    
                        showMainActivity();
                    }
                });
            }
        });
    
        databaseInitTask.execute("");
      }
    }