Arnab
Arnab

Reputation: 726

Android Application onCreate not called for homescreen launcher app during reboot

I am having a strange problem in an Android application that I am building, the application is basically a Homescreen replacement app which will be put as a default homescreen in a device. To do some initialization work I have extended Android Application class and in the onCreate() method I am basically registering some observer and starting a service, here's the code:

public class MyApplication extends Application implements ExternalStorageListener {
    private ExternalStorageObserver externalStorageObserver;

    public void onCreate() {
        Log.i("MyApplication", "Starting application");
        super.onCreate();

        externalStorageObserver = new ExternalStorageObserver(this);

        if(externalStorageObserver.isExternalStorageAvailable()) {
            // this builds a list of files present in the SD card
            // which is being used through the application to do
            // some work
            buildData();

            File externalFileDir = getApplicationContext().getExternalFilesDir(null);
            if(externalFileDir != null && externalFileDir.isDirectory()) {
                // do something...  
            } 
        }

        //Register listener to observe external storage state
        registerExternalStorageObserver(); 

        Log.i("SyncService", "Starting sync service...");
        ComponentName cmp = startService(new Intent(getApplicationContext(), SyncService.class));
        Log.i("SyncService", cmp.toString());
    }


    private void registerExternalStorageObserver() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
        filter.addAction(Intent.ACTION_MEDIA_REMOVED);
        registerReceiver(externalStorageObserver, filter);
    }

    private void buildData() {
        // builds a list
    }   
}

Content of Manifest file:

<application android:persistent="true" android:icon="@drawable/icon"
    android:label="@string/app_name" android:name="com.webgyani.android.MyApplication"
    android:debuggable="true">

    <activity android:name=".HomeTabActivity" android:launchMode="singleInstance"
        android:stateNotNeeded="true" android:theme="@style/LightTabsTheme"
        android:screenOrientation="landscape" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.HOME" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

This works fine when I install the app either using Eclipse or manually installing the apk onto the device. Once the app is installed things work fine, I mean the above onCreate() method gets called and service is also started normally, but if I reboot the device this time the onCreate() method does not get called(none of the log statements appear and also the services are not started). After some debugging I noticed that this only happens if I set my app as the default launcher/homescreen app and thereafter reboots the device, because once you set the app as the default launcher, Android should automatically launch your app as the homescreen after reboot. In my case the app is launched but that code is not executed.

I tried to use debugger but that didn't work because when I reboot the device the debugger gets disconnected and by the time USB debugging gets enabled my app is already started.

I even double checked the Logcat but didn't see any error. I thought of having a BOOT_COMPLETED intent to initialize that part, but that will require some code refactoring which I am not willing to do at this point of time unless there is a solution.

So I am curious to know that whether this is a standard behavior, or is there a known bug which causes this, because my assumption is the onCreate method of the Application will always get called whenever the app is started. I have tried whatever I could since morning, but nothing worked, couldn't pinpoint the issue, if any of you could shed some light into this then that would be highly appreciated.

Thanks

Upvotes: 2

Views: 4102

Answers (1)

Arnab
Arnab

Reputation: 726

Well, finally I figured out the problem, and it was in my code itself. I initially suspected that the MyApplication's onCreate() method was not getting called, I had that assumption because I was not able to see any logs. To know whether the method is getting called or not, instead of using Log.i() I was also appending some additional log messages in an ArrayList and printing them later, this revealed that the methods were indeed getting called and even the Service was instantiating properly but the data or filelist is not being populated because the SDCard was not ready by that time. I am also pretty sure that the logs were not available on Logcat due to the fact that the USB debugger becomes ready after my app is started(as it's a homescreen app).

The actual problem becomes obvious when you see that my overridden MyApplication class implements a listener called ExternalStorageListener which basically extends BroadcastReceiver, I have created that class to receive SDCard related Intents for example ACTION_MEDIA_MOUNTED, ACTION_MEDIA_REMOVED to rebuild the data(file list). In my case the ExternalStorageListener class was not receiving the Intents because I forgot to add this filter.addDataScheme("file") in the registerExternalStorageObserver method above in the code sample.

I do agree that my question was based on a false assumption and the code example I posted it's kind of hard to figure out the actual issue. I am not sure what to do with the question whether to mark this as answer or leave it as it is.

Upvotes: 4

Related Questions