Search code examples
androidopengl-esandroid-7.0-nougat

Activity layout is not displayed during game loading (starting with Android API 24)


There is game activity in which OpenGL rendering is performed (component app.OGlSurfaceView). While the game is loading, component textView "Loading..." is displayed on the black screen.

GameActivity.xml:

<androidx.constraintlayout.widget.ConstraintLayout 
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".activities.GameActivity">

  <app.OGlSurfaceView
    android:id="@+id/oglView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

  <androidx.constraintlayout.widget.ConstraintLayout
    android:background="@color/black">
    <TextView
      android:id="@+id/load_level_text"
      android:text="Loading..."/>
  </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

This works well on Android with API 14-23. But starting from version API 24 (Android 7.0), the load message on black screen does not appear, but continues displayed the layout of MainActivity.xml(until the game loads). Question: how to make a message appear during game loading? I will be grateful for the answers.


Solution

  • Found a workaround. Starting with android 7.0 use AsyncTask in MainActivity.

    MainActivity.kt:

    class MainActivity: AppCompatActivity(view: View) {
        ...
        fun startGameButtonClick() { // start the game
            val layout: ConstraintLayout = findViewById(R.id.loading_layout)
            Loading(layout).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
            ...
            val intent = Intent(this, GameActivity::class.java)
            startActivity(intent)
        }
    }
    

    AsyncTask.java:

    public class Loading extends AsyncTask<Void, Void, Void> {
        private WeakReference<ConstraintLayout> layout;
    
        public Loading(ConstraintLayout layout) {
            this.layout = new WeakReference<>(layout);
        }
    
        @Override
        protected void onPreExecute() {
            layout.get().setVisibility(View.VISIBLE); // show load message
        }
        ...
    }