Is there a way to dynamically change the starting activity in Android based upon a conditionally? What I attempted to do (that didn't work) was the following:
.
if (condition) { startActivity(new Intent(this, MenuActivity.class)); } else { startActivity(new Intent(this, LoginActivity.class)); }
To start an activity, use the method startActivity(intent) . This method is defined on the Context object which Activity extends. The following code demonstrates how you can start another activity via an intent. # Start the activity connect to the # specified class Intent i = new Intent(this, ActivityTwo.
To start an activity, call startActivity() and pass it your Intent . The system receives this call and starts an instance of the Activity specified by the Intent .
Why not have an initial Activity
with no UI that checks the condition in its onCreate
, then launches the next Activity
, then calls finish()
on itself? I've never called finish()
from within onCreate()
though, so I'm not sure if this will work.
EDIT
Seems to work fine. Here's some code to make it clearer.
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); }
Here's what I personally did for one of my small mobile projects. Instead of creating a separate, screen-less Activity
where the condition is and which launches the corresponding screen, I put the condition in one Activity
and did a dynamic setContentView()
, as in:
if (!userIsLoggedIn) { setContentView(R.layout.signup); } else { setContentView(R.layout.homescreen); }
Two important notes to this approach:
1: Instead of writing that in onCreate()
, you want to put the decision-making inside onResume()
precisely because the latter is always called whenever the screen needs to be displayed in front. You can see that from the Android activity life cycle. So if, for example, the user just downloaded my app and launched it for the first time, because no user is logged in, she will be led to the signup page. When she's done signing up and for some reason presses the HOME
button (not BACK
, which exits the app altogether!) and then resumes the app, the layout that she will see is already the home screen's. If I put the conditional inside onCreate()
, what would have been displayed is the sign up screen because according to the life cycle, it doesn't go back to onCreate()
when bringing back an app to the front.
2: This solution is ideal only if merging the functionalities of those two Activities would not produce a long diabolical block of code. Like I said, my project was a small one (its primary feature occurs in the background), so that single dynamic Activity
didn't have too much in it. The screen-less Activity
is definitely the way to go if you need your code to be more human-readable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With