Dynamic startup activity in Android?

Is there a way to dynamically change the initial activity in Android based on conditionally? What I tried to do (this did not work) was as follows:

  • remove the LAUNCHER category as defined in my AndroidManifest.xml
  • create your own application class that uses the application
  • override the onCreate method of my Application class to define some code as follows:

.

if (condition) { startActivity(new Intent(this, MenuActivity.class)); } else { startActivity(new Intent(this, LoginActivity.class)); } 
+43
android android-intent android-activity launcher
Jan 31 '11 at 21:50
source share
2 answers

Why not have an initial Activity without a user interface that checks the state in onCreate , then runs the next Activity , and then calls finish() on its own? I never called finish() from onCreate() , so I'm not sure if this will work.

EDIT
Seems to work well. Here is some code to make it more clear.
Initial Activity :

 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent; if (condition) { intent = new Intent(this, ClassA.class); } else { intent = new Intent(this, ClassB.class); } startActivity(intent); finish(); // note we never called setContentView() } 

Other Activity :

 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } 
+56
Jan 31 2018-11-11T00:
source share

Here is what I personally did for one of my small mobile projects. Instead of creating a separate Activity free screen where the condition is satisfied and which launches the corresponding screen, I set the condition to one Activity and made a dynamic setContentView() , as in:

 if (!userIsLoggedIn) { setContentView(R.layout.signup); } else { setContentView(R.layout.homescreen); } 

Two important points to this approach:

1: Instead of writing this to onCreate() , you want to make an exact decision inside onResume() , because the latter is always called whenever the screen should be displayed in front. You can see this on the Android activity life cycle . So, if, for example, a user has just downloaded my application and launched it for the first time, because no user has logged in, it will be sent to the registration page. When she registered and for some reason presses the HOME button (not BACK , which completely deletes the application!), And then resumes the application, the layout that she sees is already the initial screen. If I put a conditional value inside onCreate() , what would be displayed is the registration screen, because according to the life cycle, it does not return to onCreate() when the application returns to the beginning.

2: This solution is ideal only if the merging of the functions of these two Acts does not lead to the creation of a long devilish block of code. As I said, my project was small (its main function is found in the background), so there wasn’t too much for one dynamic Activity . A screen without Activity definitely suitable for making your code more human-readable.

+8
Aug 03 2018-12-12T00:
source share



All Articles