A good way to solve this is:
- Make
LoggedInActivity
anabstract class
. Add to it an abstract method:
abstract void codeThatShouldNotBeExecutedWithoutUser();
Change all subclasses to implement (and override)
codeThatShouldNotBeExecutedWithoutUser()
. If they already implement that method, you should at least add the@Override
annotation to make it clear that the method is being overridden.- Remove the calls to
codeThatShouldNotBeExecutedWithoutUser()
from all the subclasses. Change
LoggedInActivity.onCreate()
to callcodeThatShouldNotBeExecutedWithoutUser()
if and only if the user is logged in:@Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (!isUserLoggedIn()) { finish(); startActivity(getLoginActivity()); } else { codeThatShouldNotBeExecutedWithoutUser(); } }