Java Question android.app.Application - order of initializing

moster67

Expert
Licensed User
Longtime User
Not sure how to explain this....I have never seen this before..

I am wrapping a project which has a demo-app where there is a class that extends the "android.app.Application" class. Let us call this class A.
The workflow of their demo-app is the following:
1) first thing is that class A runs with code in its onCreate method (there is no layout for this class)
2) class A then starts a service
3) finally the MainActivity is started and shows its layout.

I had a look at the manifest but I cannot find anything there which says that class A should be executed first.
I, therefore, guess that a class that extends the "android.app.Application" class will always run first (it will be the entry-point).

In my wrapper, I have included the class A but it will not execute in my B4A project.

Any idea how I could get class A running at the start of the library. Do I need to wrap it as well and create the usual initial method and perhaps run it in the Starter module of B4A?

Many thanks for any help I can get.
 

DonManfred

Expert
Licensed User
Longtime User
Extending Application is problematic in B4A.

Usually i do not use it; instead in do the intialization in my Wrapper.

Can you tell which project exactly you are wrapping?
 

moster67

Expert
Licensed User
Longtime User
I was wrong about the manifest. It actually has an entry in the manifest:
B4X:
<application
        android:name=".KCTApp"

I set it and now the logs I put in class A show up. So it seems to work:
B4X:
1 - We are in KCTApp in its onCreate method
Copying updated assets files (1)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **

However, the service it should execute did not start but that might be another problem.

Anyway, here is the class A (KCTApp.java)

Java:
package com.kct.bluetooth_demo;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.util.Log;

import com.facebook.stetho.Stetho;
import com.kct.bluetooth.KCTBluetoothManager;
import com.kct.bluetooth_demo.db.DBOpenHelper;
import com.kct.bluetooth_demo.db.dao.DaoMaster;
import com.kct.bluetooth_demo.db.dao.DaoSession;

import org.greenrobot.greendao.database.Database;


/**
* KCTApplication
*/
public class KCTApp extends android.app.Application{

    private static KCTApp instance;
    private static KCTBluetoothService mKCTBluetoothService;

    private DaoSession mDaoSession;

    public static KCTApp getInstance() {
        Log.i("B4A","3 - We are in KCTApp");
        return instance;
    }

    public static KCTBluetoothService getmBluetoothLeService() {
        return mKCTBluetoothService;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        Log.i("B4A","1 - We are in KCTApp in its onCreate method

");
        KCTBluetoothManager.getInstance().init(this);

        DBOpenHelper helper = new DBOpenHelper(this, "bluetooth_demo");
        Database db = helper.getWritableDb();
        mDaoSession = new DaoMaster(db).newSession();

        Stetho.initializeWithDefaults(this);

        initBlueTooth();
    }

    public DaoSession getDaoSession() {
        return mDaoSession;
    }

    public static void initBlueTooth() {
        Intent gattServiceIntent = new Intent(instance, KCTBluetoothService.class);
        instance.bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
    }

    public final static ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder service) {
            try {mKCTBluetoothService = ((KCTBluetoothService.LocalBinder) service).getService();
                if (!mKCTBluetoothService.initialize()) {
                }
            } catch (Exception e) {e.printStackTrace();}
        }@Override
        public void onServiceDisconnected(ComponentName componentName) {
            mKCTBluetoothService = null;}
    };
}
 
Last edited:
Top