How to prevent navigation stack previous screen transition when push Flutter screen from Android activity?

32 views Asked by At

I was navigating android activity from Flutter page with method channel like this,

FlutterScreenA -> AndroidActivityA

And I was going back to another Flutter screen,

AndroidActivityA -> FlutterScreenB

This works perfectly but I have a problem. When I push AndroidActivityA -> FlutterScreenB, I see a switch from FlutterScreenA to FlutterScreenB, albeit briefly. How can I prevent that?.

----Channel helper class--------
class AppChannel {
  static AppChannel? _instance;
  static AppChannel get instance {
    return _instance ??= AppChannel._init();
  }

  AppChannel._init();

  final MethodChannel appMethodChannel = const MethodChannel("cmj_methodChannel");
}

-------- main.dart---------------------

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      routes: {
        'secondView': (context) => SecondView(),
      },
      onGenerateRoute: (settings) {
        switch (settings.name) {
          case 'secondView':
            return MaterialPageRoute(
              builder: (context) => const SecondView(), // its just empty flutter screen 
            );

          default:
            return MaterialPageRoute(
              builder: (context) => const InitView(),
            );
        }
      },
      home: const InitView(),
    );
  }
}


class InitView extends StatelessWidget {
  const InitView({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
           body: Center(child: 
           TextButton("Go",onPressed:() async{
             await AppChannel.instance.appMethodChannel.invokeMethod('toScreen');           }
            )
         ),
    );
  }
}

----------------MainActivity.kt--------


class MainActivity : FlutterActivity(), MethodCallHandler {

    companion object {
        lateinit var methodChannel: MethodChannel
        lateinit var engine:FlutterEngine
    }

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)

        engine = flutterEngine
        methodChannel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger,"cmj_methodChannel")
        methodChannel.setMethodCallHandler(this)
    }

    override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
        when(call.method){
            "toScreen" -> {
                startActivity(AndroidActivityA.createIntent(this))
            }
        }
    }
}


--------------AndroidActivityA----------------------

class AndroidActivityA: AppCompatActivity() {
   private lateinit var closeButton: Button
    
   companion object {
       
        @JvmStatic
        fun createIntent(context: Context): Intent {
            return Intent(context, AndroidActivityA::class.java)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.android_a_activity)
        closeButton = findViewById(R.id.btnClose)

        closeButton.setOnClickListener {
            MainActivity.engine.navigationChannel.pushRoute("secondView")
            finish()
        }
        
    }
}
0

There are 0 answers