Say I have 4 Activities: Login, Sync, Main, Details.
Main and Details can only be accessed if the user is logged in and passed the synchronization phase. Main can be accessed from an icon on the drawer. Details can be accessed from Main and from the notifications.
The user is logged off after 30 minutes of inactivity (with a timer on a Service).
I am not sure which way I should take to make sure the user is logged in. Here are my two scenarios:
- The icon launches
Loginwhich checks if the user is logged in, then launchesSynch, which does the same and launchesMain. - The icon launchs
Mainwhich checks is the user is logged in, if not, launchLogin.Logincloses itself to showMainthat was on the back stack.
And then there is also Details that needs to checks if the user is logged in.
The second scenario seems more correct to me, because it allows to launch other Activities than Main, but it will force me to duplicate the check code in every Activity that needs to have the user logged in. (I have more than 2 activities).
Why way should I take? Why?
You can extend a
BaseActivitywhich implements all your checking code, then any newActivityyou use will have it built in.I use this in an app which has access to 50% of the Activities without logging in and displays a prompt using
startActivityForResult(...)to catch the success or failure of the login attempt.It works really well and keeps everything neatly encapsulated 🙂
EDIT
Here is a code snippet to modify as you wish: