Android 如果应用程序未关闭(在后台运行),是否会导致应用程序崩溃?
我有一个应用程序在emulator和mobile中运行良好,但如果我们通过单击手机的退出按钮(而不是从应用程序)来关闭应用程序。几个小时后,我们将重新打开应用程序,它将从应用程序的中间(而不是从第一个屏幕)打开。在使用该应用程序若干次后,它会被挂起,并显示消息“不幸的是,应用程序已停止”。这是移动问题还是应用程序问题。实际上,该特定应用程序未正确关闭。这只是应用程序错误。我建议阅读文档。 Android操作系统有自己的应用程序生命周期管理。 在调用其onDestroy之前,每个活动都保持“活动”。例如,操作系统可以让一个活动保持活动状态几个小时,然后在没有足够内存执行其他任务时将其杀死 在您的情况下,最有可能的情况是,当您再次打开应用程序时,相同的活动重新运行(在emulator中,该活动可能在之前被终止),并且您处于错误状态,因为可能某些对象已被释放或重新初始化 正确的做法是使用其他一些状态回调,例如onPause/Resume来分配/处置活动使用的资源 您的代码可能如下所示:Android 如果应用程序未关闭(在后台运行),是否会导致应用程序崩溃?,android,Android,我有一个应用程序在emulator和mobile中运行良好,但如果我们通过单击手机的退出按钮(而不是从应用程序)来关闭应用程序。几个小时后,我们将重新打开应用程序,它将从应用程序的中间(而不是从第一个屏幕)打开。在使用该应用程序若干次后,它会被挂起,并显示消息“不幸的是,应用程序已停止”。这是移动问题还是应用程序问题。实际上,该特定应用程序未正确关闭。这只是应用程序错误。我建议阅读文档。 Android操作系统有自己的应用程序生命周期管理。 在调用其onDestroy之前,每个活动都保持“活动”
public class SomeActivity extends Activity
{
public void onCreate()
{
super.onCreate();
// Do some object initialization
// You might assume that this code is called each time the activity runs.
// THIS CODE WILL RUN ONLY ONCE UNTIL onDestroy is called.
// The thing is that you don't know when onDestry is called even if you close the.
// Use this method to initialize layouts, static objects, singletons, etc'.
}
public void onDestroy()
{
super.onDestroy();
// This code will be called when the activity is killed.
// When will it be killed? you don't really know in most cases so the best thing to do
// is to assume you don't know when it be killed.
}
}
public class SomeActivity extends Activity
{
public void onCreate()
{
super.onCreate();
// Initialize layouts
// Initialize static stuff which you want to do only one time
}
public void onDestroy()
{
// Release stuff you initialized in the onCreate
}
public void onResume()
{
// This method is called each time your activity is about to be shown to the user,
// either since you moved back from another another activity or since your app was re-
// opened.
}
public void onPause()
{
// This method is called each time your activity is about to loss focus.
// either since you moved to another activity or since the entire app goes to the
// background.
}
}
您的代码应该如下所示:
public class SomeActivity extends Activity
{
public void onCreate()
{
super.onCreate();
// Do some object initialization
// You might assume that this code is called each time the activity runs.
// THIS CODE WILL RUN ONLY ONCE UNTIL onDestroy is called.
// The thing is that you don't know when onDestry is called even if you close the.
// Use this method to initialize layouts, static objects, singletons, etc'.
}
public void onDestroy()
{
super.onDestroy();
// This code will be called when the activity is killed.
// When will it be killed? you don't really know in most cases so the best thing to do
// is to assume you don't know when it be killed.
}
}
public class SomeActivity extends Activity
{
public void onCreate()
{
super.onCreate();
// Initialize layouts
// Initialize static stuff which you want to do only one time
}
public void onDestroy()
{
// Release stuff you initialized in the onCreate
}
public void onResume()
{
// This method is called each time your activity is about to be shown to the user,
// either since you moved back from another another activity or since your app was re-
// opened.
}
public void onPause()
{
// This method is called each time your activity is about to loss focus.
// either since you moved to another activity or since the entire app goes to the
// background.
}
}
底线:始终假设相同的活动可以再次运行。请在问题中添加代码,至少您应该发布错误日志的cat日志。.是的,但在emulator中工作正常,有时(很少)我在手机上收到这条消息。所以我看不到logcat。你能告诉我如何正确关闭应用程序吗?你必须从退出按钮或后退按钮关闭应用程序。然后,只有应用程序正确关闭。您可以在任务管理器中检查应用程序是否仍在运行。请给我一个关于“正确的做法是使用其他一些状态回调,例如onPause/Resume来分配/处置活动使用的资源”的示例。当然,请查看答案。只是做了一些改变。