Quantcast
Channel: Active questions tagged crash - Stack Overflow
Viewing all articles
Browse latest Browse all 7187

Android Application onCreate is not called before Activity onCreate

$
0
0

On Google Play I receive NullPointerException, on trying to access static Application instance. It appears on different Android OS versions: Android 9/8/6

From code it seems, Application.onCreate() hasn't been called / called after Activity.onCreate().

I saw similar questions but without any useful response: https://stackoverflow.com/questions/53607129/application-oncreate-callednot-called-after-activity-oncreate

https://stackoverflow.com/questions/56795368/android-activityoncreate-called-before-application-oncreate

As side solution, I'm going to create separate Singleton class depending on context, and get it with it (Context), but not sure, how it will work with Dagger in future.

My question is: 1. Is it Android bug? 2. Am I doing something wrong? 3. How other developers solve it?

public class App extends androidx.multidex.MultiDexApplication implements Application.ActivityLifecycleCallbacks, ComponentCallbacks2 {
    private static App instance;

    public static App getInstance() {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;

        // init singletone
    }

    private Singletone singletone;

    public Singletone getSingletone() {}
}

public class MainActivity extends androidx.appcompat.app.AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // On Google Play it produces NullPointerException

        Singletone singletone = App.getInstance().getSingletone();
    }
}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.package.name">
    <application
        android:name=".app.App"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme.NoActionBar"
        android:largeHeap="true">

        <activity
            android:name=".activities.MainActivity"
            android:screenOrientation="portrait"
            android:launchMode="singleTask"
            android:windowSoftInputMode="adjustPan"
            android:theme="@style/AppTheme.NoActionBar.Launcher">
        </activity>
    </application>
</manifest>

Viewing all articles
Browse latest Browse all 7187

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>