立即执行后台代码,我的应用程序完全终止,再也不会



我正在实现我的群组语音通话应用程序的在线/离线功能。当用户按下菜单(打开设备上所有打开的应用程序(并滑动我的应用程序时,我想从活动用户升降机中删除用户。所以我可以在那里发射一个火球功能来移除。现在我只能处理willpop和应用程序在后台的时候。

尝试在dispose方法中编写该代码。

您应该使用">didChangeAppLifecycleState";

class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
// add the observer
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
// remove the observer
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
// These are the callbacks
switch (state) {
case AppLifecycleState.resumed:
// widget is resumed
break;
case AppLifecycleState.inactive:
// widget is inactive
break;
case AppLifecycleState.paused:
// widget is paused
break;
case AppLifecycleState.detached:
// widget is detached
break;
}
}
@override
Widget build(BuildContext context) => Scaffold();
}

始终停用AppLifecycleState.paused上的用户,并在AppLifecycleState.resumed中再次激活用户

最新更新