I'm a flutter beginner and I've tried to implement a splash screen using launch_background.xml as bellow:

<?xml version="1.0" encoding="utf-8"?>
<!-- Modify this file to customize your launch splash screen -->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/white" />

    <!-- You can insert your own image assets here -->
    <item>
        <bitmap
            android:gravity="center"
            android:src="@mipmap/ceb_launcher" />
    </item> -->
</layer-list>

The splash screen is working fine. When I'm at home screen (that can be a login screen or another screen if the login was successful) and I press the back button the app minimizes (enters at detached state) and when I reopen the app, it gets stuck at splash screen. If I close the app and open it again the splash screen appears for some seconds and then I go to home page.

No erros is shown at debug console just this when the back button is pressed:

E/libEGL  (28992): call to OpenGL ES API with no current context (logged once per thread)

And this when I return and the app gets stuck at splash screen:

D/EGL_emulation(28992): eglCreateContext: 0xe3b654c0: maj 2 min 0 rcv 2
D/EGL_emulation(28992): eglCreateContext: 0xe3b65520: maj 2 min 0 rcv 2
D/        (28992): HostConnection::get() New Host Connection established 0xdb871800, tid 29168
D/EGL_emulation(28992): eglMakeCurrent: 0xe3b65520: ver 2 0 (tinfo 0xdb816c30)
D/        (28992): HostConnection::get() New Host Connection established 0xdb899640, tid 29167
D/EGL_emulation(28992): eglMakeCurrent: 0xe3b654c0: ver 2 0 (tinfo 0xdb883bb0)
D/EGL_emulation(28992): eglCreateContext: 0xdb885060: maj 2 min 0 rcv 2
D/EGL_emulation(28992): eglMakeCurrent: 0xdb885060: ver 2 0 (tinfo 0xdb883bb0)
D/EGL_emulation(28992): eglMakeCurrent: 0xe3b654c0: ver 2 0 (tinfo 0xdb883bb0)
D/EGL_emulation(28992): eglMakeCurrent: 0xdb885f60: ver 2 0 (tinfo 0xc9596880)

Here is a piece of my code:

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(AppWidget());
}

class AppWidget extends StatelessWidget {

  @override
  Widget build(BuildContext context){

    SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);

    return MultiProvider(
      providers: [
        ChangeNotifierProvider.value(value: AuthenticationBLOC(),),
        ChangeNotifierProvider.value(value: EquipmentBLOC()),
        ChangeNotifierProvider.value(value: ScreenDrawerBLOC()),
      ],
      child:Consumer<AuthenticationBLOC>(builder: (ctx, autentication, _) => MaterialApp(
        debugShowCheckedModeBanner: false,
        theme: themeAllApp(),
        home: isAuthenticated(autentication),
        onGenerateRoute: routesApp(context)
        ),
      ),
    );
  }
}

Widget isAuthenticated(AuthenticationBLOC autentication){
  if(autentication.authenticated != true){
    return FutureBuilder(
      future: autentication.tryAutoLogIn(),
      builder: (context, snapshot) => snapshot.connectionState == ConnectionState.waiting ? 
        LoadingScreenWidget() : LogInScreenWidget(),
    );
  }else{
    return Consumer<ScreenDrawerBLOC>(
      builder: (context, screen, _) => screen.isTypeScreen ? TypeDeviceScreenWidget() : 
        ContactScreenWidget(),
    );
  }
}

I've already ran flutter flutter doctor and it's ok everything.

If anyone has an ideia about what's going on I'll appreciate.


Solution 1: Michée Nonga

I had the same problem my flutter app. When you go back to the app from the launcher icon after leaving the app by hitting the back button, the app get stuck at splash screen.

I used native code on android to resolve the problem:

void main() async {
    runApp(Builder(builder: (context) => MaterialApp(title: 'Welcome to Flutter', home: Home3())));
}

class Home3 extends StatefulWidget {
  Home3({Key? key}) : super(key: key);

  @override
  _Home3State createState() => _Home3State();
}

class _Home3State extends State<Home3> {
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        var _androidAppRetain = MethodChannel("android_app_retain");
        print("on will pop");
        try {
          if (Platform.isAndroid) {
            print("plateform is android");
            if (Navigator.of(context).canPop()) {
              print("can pop");
              return Future.value(true);
            } else {
              var result =
                  await _androidAppRetain.invokeMethod("sendToBackground");
              print("result is $result");
              return Future.value(false);
            }
          } else {
            print("plateform is ios");
            return Future.value(true);
          }
        } catch (e) {
          print("Error: $e");
          return Future.value(true);
        }
      },
      child: Scaffold(
        appBar: AppBar(
          title: const Text('Welcome to Flutter'),
        ),
        body: const Center(
          child: Text('Hello World'),
        ),
      ),
    );
  }
}

native kotlin code:


import io.flutter.embedding.android.FlutterActivity

import android.os.Build
import android.view.ViewTreeObserver
import android.view.WindowManager
import io.flutter.plugins.pathprovider.PathProviderPlugin

import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import kotlin.system.exitProcess

class MainActivity: FlutterActivity() {
    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
    super.configureFlutterEngine(flutterEngine)
    MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "android_app_retain").setMethodCallHandler {
      call, result ->
      if (call.method == "sendToBackground") {
                    finishAndRemoveTask();
                    result.success(0);
                    exitProcess(0);
                }
    }
  }
}