Reputation: 2079
I have two activities but I want to apply a condition on application start, if one is true then start first activity else start second activity. Right now I am doing it by starting a third activity displaying some welcome stuff and examining the value in background..then appropriate activity gets called. I think there must be some standard way to do this. Peace.
Upvotes: 1
Views: 303
Reputation: 48871
There's no need for a third Activity
to check the condition.
If you simply have your MAIN/LAUNCHER Activity
check on the condition as the very first thing it does in onCreate(...)
(but after calling super.onCreate(...)
), it can either continue or call startActivity(...)
for the other Activity
and immediately call finish()
to self-terminate.
That way the first Activity
will never be seen if the condition dictates the second Activity
should be started.
Example..
public class FirstActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Don't even set the content view at this point
// we want to be invisible for the moment
// Pseudo-code check for condition
if (!runMe) {
Intent i = new Intent(this, SecondActivity.class);
startActivity(i);
finish();
}
else {
// Continue as normal
setContentView(R.layout.main);
...
}
}
}
Upvotes: 2
Reputation: 20944
Since you need to specify starting activity in manifest, you can always start first activity, check your condition on onCreate()
and if you need to start second one - start second one and call finish()
for the first activity.
Otherwise usually people use splash activity to check all the conditions at the startup (which is your current solution).
Upvotes: 2