Java – Is it possible not to recreate an activity after manually locking the phone?

Is it possible not to recreate an activity after manually locking the phone?… here is a solution to the problem.

Is it possible not to recreate an activity after manually locking the phone?

I’m trying to make a simple openGL ES app (currently working on it), onStop triggers automatically when I lock my phone or click the back button, when I unlock or open the app from a task, the Activity recreates itself and the triangle (from android studio opengl es beginner documentation) is broken.

I tried to record when the app triggers onCreate :

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

Log.w("ONCREATE", "Hello, something happened here.");

getWindow().getDecorView().setSystemUiVisibility(
             View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                     | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                     | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                     | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                     | View.SYSTEM_UI_FLAG_FULLSCREEN
                     | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
     mSurface = new SurfaceView(this);
     setContentView(mSurface);
}

I want

my application not to recreate the activity (I want it to be created when it actually starts, not again).

Thank you.

Solution

No, you can’t prevent activities from being recreated when the screen is off or the user is away.

I recommend that you official Android Fundamentals Start your Android journey. Understand the lifecycle of Android applications in a restricted environment with a Linux operating system running on a restricted device (phone/tablet).

When the activity/application stops, you should stop rendering/releasing your OpenGL surface to stop using the user resource and recreate it when you come back.

Related Problems and Solutions