I am creating an app having a navigation drawer activity with fragments. At every cold start of the app, I am executing some initialization code where I load the following things:
- The user session(if the user is logged in or not)
- Registering Retrofit services
- Getting some data from the server to proceed with startup of the app.
This is the flow of my app when doing a cold start:
- Starting MainActivity and verifying the user session.
- If the session is valid, then we open the CoreActivity.
- If not, then we open the LoginActivity.
When the app is brought to the foreground after some inactivity Android tries to restart the current Activity. This means my initialization code is bypassed and CoreActivity.onCreate()
is executed.
All my activities(except MainActivity) are extending the following super activity:
public abstract class MasterActivity extends AppCompatActivity {
@Override
protected final void onCreate(Bundle savedInstanceState) {
this.supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
if (!CrmContext.getInstance().verifyContextSet(this)) {
return;
}
super.onCreate(savedInstanceState);
onCreateAfterContext(savedInstanceState);
}
In CrmContext:
public boolean verifyContextSet(final Context context) {
boolean isContextSet = applicationContext != null;
if (isContextSet) {
return true;
}
Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
return false;
}
In verifyContextSet()
I am doing some checks to be sure that the app has been correctly loaded. If the user session is not properly loaded.
My problem:
If the app is brought to the front the CoreActivity.onCreate()
is executed and verifyContextSet()
returns false. In this case I want to cancel the creation of CoreActivity and open MainActivity again.
When I do my verifyContextSet()
before super.onCreate()
, then I get this exception:
android.util.SuperNotCalledException: Activity {nl.realworks.crm/nl.realworks.crm.view.CoreActivity} did not call through to super.onCreate() at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2287) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391) at android.app.ActivityThread.access$800(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
I tried to execute super.onCreate()
first, but then the Fragment
inside the activity is created first. This means that my Fragment
is recreated before my verifyContextSet()
is executed.
So, If I try to cancel()
/finish()
the onCreate()
before super.onCreate()
has been called, then I get the SuperNotCalledException
. If I execute super.onCreate()
first, then the Fragment
is initialized which is not allowed when verifyContextSet()
returns false
.
I want to do the following:
- Bringing the app to the foreground
- Check if the app has been initialized
- If not, then
finish()
the current activity and then restart the app to open MainActivity.