Service freezing activity before UI has inflated

Viewed 70

I have a long-running service that is bound to a loading screen. I know this is suboptimal, but an AsyncTask did not work for me as it keeps running (data leaks) even after the Activity is ended forcefully. Regardless, want to understand what is happening here. The basic layout never shows up (black screen), but the service runs as intended. A (bound) service lives and runs on the activity thread, of course. But why does it not inflate the UI first? onCreate gets called before bindService afaik and as the Log confirmed.

 ServiceConnection _connection = new ServiceConnection() {
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            _service = myService.Stub.asInterface(iBinder);
            runStuff();
        }

        public void onServiceDisconnected(ComponentName componentName) {
            _service = null;
        }

@Override
    protected void onStart() {
        super.onStart();
        Intent serviceIntent = new Intent();
        serviceIntent.setPackage(myService.class.getPackage().getName());
        serviceIntent.setAction(myService.class.getName() + ".ACTION_BIND");
        bindService(serviceIntent, _connection, BIND_AUTO_CREATE);
    }
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_loadingscreen);
        findViewsById();
        setDefaultVisibility();
    [...]

Edit: As additional info, it actually takes a long time to display the (very simple) loading screen even with the AsyncTask. But even that weird delay should still not matter, because the service should not bind before then. What am I missing?

Edit2: I added timestamps. The service starts immediately. If the service is in an AsyncTask, the UI still takes the same time to load. From the button tap on the previous activity to onPostCreate it takes about 400ms, not the 3-4 s I see looking at the UI. The AsyncTask does update the UI as expected, but apart from onProgressUpdate, there is nothing pertaining to UI after onCreate or before any service calls. What could cause such a delay? I don't even have that on activities in the same app that are heavy on DB and GUI.

1 Answers

Put your code in a seperate method, I dont know if this will work but i have had success like this

private void start(){
 Intent serviceIntent = new Intent();
        serviceIntent.setPackage(myService.class.getPackage().getName());
        serviceIntent.setAction(myService.class.getName() + ".ACTION_BIND");
        bindService(serviceIntent, _connection, BIND_AUTO_CREATE);
}

and then start that method in your oncreate the service will then start after creation like

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_loadingscreen);
            findViewsById();
            setDefaultVisibility();
start();
        [...]
Related