Maxim M
Maxim M

Reputation: 336

blocking UI while loading static C++ library in Android

My UI thread is blocking, while I'am loading static C++ library. I want to create a spinning loading bar, so user don't think App is freezed. But my bar is not spinning due to this library loading(it takes like 5 seconds). Is it possible to load library in static block and not update UI?

public class LoadingActivity extends AppCompatActivity {


    /**
     * Load native libraries
     */

    static {
        System.loadLibrary("native-lib");
        if (BuildConfig.DEBUG) {
            OpenCVLoader.initDebug();
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent returnIntent = new Intent();
        returnIntent.putExtra("result", Activity.RESULT_OK);
        setResult(Activity.RESULT_OK, returnIntent);
        finish();
    }
}

Upvotes: 0

Views: 859

Answers (1)

ospf
ospf

Reputation: 300

This should do it:

static {
  new Thread(() -> {        
    System.loadLibrary("native-lib");
    if (BuildConfig.DEBUG) {
        OpenCVLoader.initDebug();
    }
  }).start();
}

But you have to care about waiting until the library is loaded and initialized before using it.

Upvotes: 1

Related Questions