Android 在对其调用finish()后重新创建活动
我正在开发一个voip应用程序,它基本上使用一个主活动(XTabsContainer)和一个服务(NativeService)。主活动始终使用标志FLAG_Activity_SINGLE_TOP和FLAG_Activity_NEW_TASK启动(即从SplashScreen活动开始) 在主活动中,我有一个选项菜单,其中只有一项用于使用以下功能退出应用程序:Android 在对其调用finish()后重新创建活动,android,android-activity,Android,Android Activity,我正在开发一个voip应用程序,它基本上使用一个主活动(XTabsContainer)和一个服务(NativeService)。主活动始终使用标志FLAG_Activity_SINGLE_TOP和FLAG_Activity_NEW_TASK启动(即从SplashScreen活动开始) 在主活动中,我有一个选项菜单,其中只有一项用于使用以下功能退出应用程序: public void exit(){ Log.d(TAG, "exit()"); mExitThread = new Ex
public void exit(){
Log.d(TAG, "exit()");
mExitThread = new ExitThread();
mExitThread.start();
this.finish();
}
ExitThread()只是关闭一些内容并终止服务:
private class ExitThread extends Thread
{
public void run() {
Log.d(TAG, "Starting Unregistration thread");
INgnSipService sipService = Engine.getInstance().getSipService();
int count = 0;
if (sipService.isRegistered())
{
sipService.unRegister();
while (count <= NativeService.mTimeout && sipService.isRegistered())
{
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
count++;
}
// Exit for timeout
if (count > NativeService.mTimeout)
Log.w(TAG, "Exiting Unregistration thread for TIMEOUT");
else
Log.d(TAG, "Exiting Unregistration thread");
}
if (!Engine.getInstance().stop()) {
Log.e(TAG, "Failed to stop engine");
}
}
}
您正在使用
Handler.post()
,这意味着ExitThread
中的代码实际上没有在另一个线程中运行,而是在主(UI)线程上运行。在ExitThread
中有一个循环处于休眠状态。这不适合在UI线程上运行,因为它会阻塞UI。Android最终将通过ANR(应用程序无响应)错误终止此活动
您需要在单独的线程中实际运行ExitThread
中的内容。而不是:
mHandler.post(new ExitThread());
做:
为什么不在你的
onDestroy()
中进行清理,让Android处理活动生命周期呢?发布你的ExitThread的内容。此外,logcat中是否还有其他有趣的消息(崩溃、错误等)?不要过滤日志,因为你可能会错过一些重要的内容。@tolgap谢谢,但在我的应用程序中,即使主活动被破坏,服务也可以运行。只有当用户真的想退出应用程序时,它们才需要被销毁。好吧,但你的问题是你想异步关闭你的东西。这意味着您的活动将在ExitThread仍在运行和清理时完成。不要将其设为线程,或者让您的活动等待来自线程的回调ExitThread@DavidWasser:添加了ExitThread的代码。不幸的是,我没有保存未过滤的日志。如果我再次复制,我会更新帖子。谢谢
mHandler.post(new ExitThread());
new ExitThread().start();