Android : App restarts from wrong activity

on Tuesday, April 7, 2015


I open my app. It launches an activity which acts as a splashscreen (ASplashscreen) in which I load some JSON data from local storage (raw folder) and store it in memory in a singleton object (static). After this process is done it automatically moves along to the main activity (AMain)


I exit the app by pressing the home buttonand run other applications, games, etc. When I reopen my app, the app crashes inside the onCreate method of the AMain because it tries to use some of the data inside the singleton object but the data is null. So it throws a NullPointerException when it does so. It appears that it restarts the AMain instead of ASplashscreen so the singleton doesn't have a chance to reinitialize.


This happes randomly across multiple such tries...


I have two presumptions ...




  1. My first presumption, and from what I know about the Android OS, is that while I was running those other applications (especially the games) one of them required a lot of memory so the OS released my app from memory to make room, so the singleton data was garbage collected.




  2. I also presume that while the gc removed my singleton from memory, the OS still kept some data relating to the "state" of the current running activity, so it knew at least that it had the AMain activity opened before i closed the app. This would explain why it reopened the AMain activity instead of the ASplashscreen.




Am i right ? Or is there another explanation why I get this exception ? Any suggestions/clarifications are welcomed.


Also, what would be the best approach to handle this ? My approach is to check the existence of he singleton data whenever i try to use it and if it's null then just basically restart the app. This makes it go through the ASplashscreen so the JSON gets initialized and everything is ok.


EDIT As requested, here's my AndroidManifest



<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.android.vending.BILLING"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<application
android:name=".global.App"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:theme="@style/AppTheme">

<!--SPLASH SCREEN-->
<activity
android:name=".activities.ASplashscreen"
android:label="@string/app_name"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

<!--MAIN-->
<activity
android:name=".activities.AMain"
android:label="@string/app_name"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="@style/AppTheme"/>

<!--MENU-->
<activity
android:name=".activities.AMenu"
android:label="@string/app_name"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="@style/AppTheme"/>

<!--HELP-->
<activity
android:name=".activities.AHelp"
android:label="@string/app_name"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="@style/AppTheme"/>

<!--ADMOB-->
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent"/>

<!--FACEBOOK LOGIN ACTIVITY (SDK)-->
<activity
android:name="com.facebook.LoginActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/AppTheme"/>

<!--This meta-data tag is required to use Google Play Services.-->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>

<!--FACEBOOK STUFF-->
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id"/>

<!--GOOGLE PLUS-->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>

<!--CRASHLYTICS-->
<meta-data
android:name="com.crashlytics.ApiKey"
android:value="9249....."/>

</application>

0 comments:

Post a Comment