Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android stop activity in onCreate() before calling super.OnCreate()

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.
like image 219
com2ghz Avatar asked Mar 25 '16 12:03

com2ghz


People also ask

What will happend when you call finish () in onCreate?

As per official documentation: You can call finish() from within this function, in which case onDestroy() will be immediately called after onCreate(Bundle) without any of the rest of the activity lifecycle (onStart(), onResume(), onPause(), etc) executing.

What is the purpose of super onCreate () in android?

Q 9 – What is the purpose of super. onCreate() in android? The super. onCreate() will create the graphical window for subclasses and place at onCreate() method.

Why is onCreate called twice?

OnCreate will only be called one time for each lifetime of the Activity. However, there are a number of situations that can cause your activity to be killed and brought back to life. Thus, onCreate will be called again.

Is onCreate only called once?

@OnCreate is only for initial creation, and thus should only be called once. If you have any processing you wish to complete multiple times you should put it elsewhere, perhaps in the @OnResume method.


1 Answers

put your checking/validating code in an Application sub class

public class MyApp extends Application {
//in your oncreate create sessions etc.

now whether MainActivity restarts or not, you have already validated.

Note: Application class' onCreate() is the firs to run before any body.

like image 91
Elltz Avatar answered Oct 12 '22 09:10

Elltz