Search code examples
androidmultithreadingandroid-activityclient

Activity freezing on startup while another thread is doing work


Edit: As pointed out by Mike M. and Vladyslav Matviienko and Vivek Mishra

new Runnable().run(); 

is not a seperate Thread. Thank you guys :)

Edit End.

When I start a new Activity that uses a separate Thread to communicate with a Server it freezes.

I start a new Activity with

Intent i = new Intent(this, AcmActivity.class);
startActivityForResult(i, acm_ui);

then I run an asynchronous call to my client class in onCreate()

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.acm);

        //get the client implementation
        ClientImpl client = ServiceManager.getService(ClientImpl.class);
        client.getData(new PrivateClientCallback())
}

private class PrivateClientCallback implements GeneralCallback {
        @Override
        public void ok(final String response) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    updateSomeView(response);
                }
            });
        }
}

The ClientImpl.getData() looks like this:

public synchronized void getData(GeneralCallback cb) {
        new Runnable() {
            @Override
            public void run() {
                //allow networking within this Thread
                //read more here: https://stackoverflow.com/questions/25093546/android-os-networkonmainthreadexception-at-android-os-strictmodeandroidblockgua
                if (android.os.Build.VERSION.SDK_INT > 9) {
                    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                    StrictMode.setThreadPolicy(policy);
                }

                //send some stuff to server and use the Callback
                String stuff = someStuff();
                cb.ok(stuff);
            }.run();
}

Unfortunately my Activity freezes until the Call from the Server returned.

I would expect the Activity to start and when the server answers to update its views, unfortunately that is not what happens. I have no idea why.


Solution

  • new Runnable() is just a normal object. You need to create a new Thread with a runnable object. Then it'll run on a separate thread.

    Check the code below

    public synchronized void getData(GeneralCallback cb) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    //allow networking within this Thread
                    //read more here: https://stackoverflow.com/questions/25093546/android-os-networkonmainthreadexception-at-android-os-strictmodeandroidblockgua
                    if (android.os.Build.VERSION.SDK_INT > 9) {
                        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                        StrictMode.setThreadPolicy(policy);
                    }
    
                    //send some stuff to server and use the Callback
                    String stuff = someStuff();
                    cb.ok(stuff);
                }).start(); 
    }