How to change the startup activity in android [closed]
-
20-06-2021 - |
Question
For the first time use of the application I will show the user with a login screen which i will defined in manifest file as "android.intent.action.MAIN". After a successful login, each time application starts I want the user to see the home screen. Please let me know how i can achieve this.
Also please let me know is there any way I can change the MAIN activity programmatically after a successful login, So that I can redirect to the home screen.
Thanks,
Vijay
Solution
What if you subclass Application
class and call the activity you need from your Application's onCreate()
? And in the manifest you remove that intent.MAIN
.
OTHER TIPS
I don't think you can set the main activity programmatically, but you can set a boolean in your sharedPreferences
. If this boolean is true
, then you call your HomeActivity
, and finish your LoginActivity
.
Hope this helps
I would, from the login screen simply call the home screen. Or from the home screen, if the login details are not saved/ valid, then show the login screen.
Basically, it is not necessary or a good programming practice to do what you are asking.
When you have successfully logged in, call finish();
in the main activity which will remove it from the stack. Then in your HomeScreenActivity, override the back button to act as the home button:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}