I need to run a specific Activity from Application (depending on the settings). Actually activates and runs .. but also activates the activit that is specified in the manifest. Those. There are 2 activations in the stack. LAUNCHER-activit cannot be removed from the manifest.

    1 answer 1

    Purpose: If you are logged in to your account, then we launch the main activity of the application, otherwise - login activity:

    Method 1

    We create additional activity, which will be the entry point of the application and we are already launching the necessary activity in it.

    public class LauncherActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Class target; if (Prefs.getInstance().isLoggedIn()) { target = MainActivity.class; } else { target = LoginActivity.class; } Intent intent = new Intent(this, target); startActivity(intent); finish(); } } 

    Register it in the manifest as LAUNCHER and remove it from the others.

    Method 2

    We leave the MainActivity as the entry point and check if the user has logged in, if not - we drop it on LoginActivity:

     public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if(!Prefs.getInstance().isLoggedIn()) { startActivity(new Intent(this, LoginActivity.class)); finish(); //обязательно } setContentView(R.layout.activity_main); // ... остальной код 

    Method 3.

    Actually, the opposite. We make LoginActivity an input point, if the user is logged in - we launch the main one.

     public class LoginActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if(Prefs.getInstance().isLoggedIn()) { startActivity(new Intent(this, MainActivity.class)); finish(); //обязательно } setContentView(R.layout.activity_login); // ... остальной код 

    Method 4 (bad practice, unreliable)

    We leave MainActivity as an entry point, but we check the Application , if it isn’t logged in, then we throw it at LoginActivity (vice versa)

     public class App extends Application { @Override public void onCreate() { super.onCreate(); if(!Prefs.getInstance().isLoggedIn()){ Intent intent = new Intent(this, LoginActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // этот флаг очистит стек активностей при переходе startActivity(intent); } } } 

    I recommend using the first option, as it is more logical, activity plays the role of branching. If the logic becomes more complicated, then the second / third option will become a burden that will make your eyes callous.

    Suggest your solutions))

    • Well, it's like an obvious solution. I just didn’t want to introduce extra activation, which is needed only to launch others. - Roman
    • Unfortunately, other methods are impossible, because the android launcher must be activated - Flippy
    • @ Roma, added the answer - Flippy