当RAM内存中清除的Andr​​oid应用程序崩溃 [英] Android Application Crashes when RAM Memory Cleared

查看:199
本文介绍了当RAM内存中清除的Andr​​oid应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的RAM内存被清除后崩溃的应用程序。我不能使用 onSavedInstanceState ,因为目前执行的。因此,没有任何人知道,怎么可能当用户试图从最近的应用打开了,我只是重新启动应用程序?我已经试过这code的主要活动是所有活动的基类:

 如果(isFirstApplicationStartUp()){
        意图I =新意图(这一点,Main.class);
        startActivity(ⅰ);
    }


  

isFirstApplicationStartUp()是一个布尔值从一个类设置为true延伸应用程序(在的onCreate)。


但可根据需要为previous活动得到执行此code之前调用这个实现不工作。

我会很感激,如果有人可以帮助我解决这个。先谢谢了。


解决方案

您可能不希望有来自当从近期任务列表正在启动的开始重新启动应用程序,因为你的应用程序可能是完全有能力加工。你需要做的是,你需要记住,如果您的应用程序已被正确初始化(是什么意思。如果用户返回到您的应用程序和进程已经死亡,重新启动,因为您的应用程序初始化,需要检测这个条件,然后将用户重定向回您的应用程序的第一个活动。

要做到这一点,最好的办法是让您的所有活动的基类。在这个基类你实施的onCreate code()来检查,如果您的应用程序已被正确初始化。如果没有正确初始化,您应该将用户重定向回的第一个活动。事情是这样的:

  @覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    //检查应用程序已被重新启动AndroidOS它已经杀死的过程中,由于不活动
    //在这种情况下,我们需要重定向到的第一个活动,并转储所有其他活动中的任务堆栈
    //以确保应用程序正常初始化
    如果(isApplicationInitialized()及!&放大器;!(此的instanceof FirstActivity)){
        意图firstIntent =新意图(这一点,FirstActivity.class);        firstIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //因此,所有其他活动将倾
        startActivity(firstIntent);        //我们都做了,所以完成这个活动,现在出去
        完();
        返回;
    }    //现在调用活动专有的onCreate()
    doOnCreate(savedInstanceState);

所有的活动都需要从这个基类继承和他们不应该重写的onCreate()。相反,他们应该实现方法 doOnCreate(),它将从基类的被称为的onCreate()(见上文)。

请注意:这只有在根系活力(在这个例子中 FirstActivity )将永远不会结束,直到应用程序退出工作。这意味着你将永远有 FirstActivity 的一个实例,在你的任务的根源。这是必需的,以便 Intent.FLAG_ACTIVITY_CLEAR_TOP 正常工作。

I have an app that crashes after the RAM memory is cleared. I cannot use the onSavedInstanceState because of the current implementation. So, does anybody know, how could I just restart the app when the user tries to open it from Recent Apps? I already tried this code in the Main activity which is the base class for all activities:

if (isFirstApplicationStartUp()) {
        Intent i = new Intent(this, Main.class);
        startActivity(i);
    }

isFirstApplicationStartUp() is a boolean set to true from a class which extends Application (in onCreate).

But this implementation does not work as desired as the previous activities get to be called before this code is executed.

I would be very grateful if anyone could help me solving this. Thanks in advance.

解决方案

You probably don't want to have the app restart from the beginning when being launched from the "recent tasks" list, because your app may be perfectly capable of working. What you need to do is you need to remember if your app has been properly "initialized" (whatever that means. If the user returns to your app and the process has been killed and restarted since your app was initialized, you need to detect this condition and then redirect the user back to the first activity of your app.

The best way to do this is to have a base class for all of your activities. In this base class you implement code in onCreate() that checks if your app has been properly initialized or not. If it has not been properly initialized, you should redirect the user back to the first activity. Something like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Check if the application has been restarted by AndroidOS after it has killed the process due to inactivity
    //  in this case, we need to redirect to the first activity and dump all other activities in the task stack
    //  to make sure that the application is properly initialized
    if (!isApplicationInitialized() && !(this instanceof FirstActivity)) {
        Intent firstIntent = new Intent(this, FirstActivity.class);

        firstIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // So all other activities will be dumped
        startActivity(firstIntent);

        // We are done, so finish this activity and get out now
        finish();
        return;
    }

    // Now call the activity-specific onCreate()
    doOnCreate(savedInstanceState);

All of your activities need to inherit from this base class and they should NOT override onCreate(). Instead, they should implement the method doOnCreate(), which will be called from the base class' onCreate() (see above).

NOTE: This only works if the root activity (in this example FirstActivity) is never finished until the app quits. This means that you will always have an instance of FirstActivity at the root of your task. This is required so that Intent.FLAG_ACTIVITY_CLEAR_TOP works correctly.

这篇关于当RAM内存中清除的Andr​​oid应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆