java - Instance of Singleton null -
i use singletons in android app. globalapp (extends application) class in oncreate() initialized this:
public class globalapp extends application{ @override public void oncreate() { super.oncreate(); databasehelper databasehelper = new databasehelper(getapplicationcontext()); mysingleton.init(databasehelper); } } and singleton:
public class mysingleton{ static mysingleton instance; databasehelper databasehelper; public static void init(databasehelper databasehelper){ instance = new mysingleton(databasehelper); } } i receive crashes app show instance null while app runs. weird thing: crashes happen on samsung galaxy s5!
my understanding
the
oncreate()method ofapplicationclass guaranteed called when app startedthe instance on singleton never become null, except when app restarted (in case should recreated
applicationclass).
what's going on here?
ps: know singletons kinda frowned upon , extending application class not recommended everyone, that's not point here.
edit: clarification, code works 99% of time. question not asking on how implement singleton pattern rather how lifecycle of (or any) app can lead instance of singleton becoming null when created in application's oncreate().
please check manifest file inside application have put android:name="" like:
<application android:name=".application.doodhwaalaapplication" android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" >
Comments
Post a Comment