应用onCreate()每次都不会被调用

时间:2015-05-15 12:57:38

标签: android android-lifecycle

应用启动时,我有一个非常重要的初始化。我发现最好的方法是将代码放在扩展Application类的类的onCreate()中。

class ApplicationDemo extends Application{

@Override
public void onCreate(){
 super.onCreate();
 Log.d("LOG", "Inside onCreate()");
}

}

问题

我没有找到每次运行应用程序时都要执行的日志语句。就像我第一次启动应用程序一样,获取日志语句,然后关闭应用程序并从启动器再次启动。不,日志声明没有来。

有什么问题?如何在每次运行应用程序以及执行其他任何操作之前确保运行特定代码?

7 个答案:

答案 0 :(得分:4)

我的猜测是您真正打开了应用一次

我非常确定,在您关闭您的应用程序之后,它真正进入后台,等待再次进入前台。 (它不会再次创建,您只能重复使用已创建的内容。)

在重新打开之前,请确保您确实killed the process of your application;确保你真的关闭&重新打开它,而不只是做一个简单的背景前景。

答案 1 :(得分:1)

这听起来像Android活动生命周期问题。 我已经包含了关于暂停和恢复活动的链接

http://developer.android.com/training/basics/activity-lifecycle/pausing.html

看起来当您退出应用时,您的活动正在暂停。同样,当您重新进入应用程序时,如果进程仍在运行,则活动将恢复而不是重新创建。

从这里,您可以将代码移动到onResume(),也可以将其保留在onCreate()中,但请确保退出应用程序会终止进程。这可以通过放

来完成
getActivity().finish();
System.exit(0);

在任何指引用户进入主屏幕的路径中(onBackPressed(),退出按钮等)

开始时,尝试在onResume中放入一个Log语句,并观察两者被调用的位置。

我希望这会有所帮助。

答案 2 :(得分:0)

在Android中,您通常不会关闭'一个应用程序,而是暂停它。 所以,当你再次运行它时,它会弹回来。 要确保您的应用已关闭,请打开正在运行的应用列表,找到您的应用并强制停止。

答案 3 :(得分:0)

即使没有显示UI,也可以存在应用程序或活动。只有在重新创建对象时才会调用onCreate()回调。

  

这仅表示“每次用户从启动器图标打开应用程序”。

然后你应该把代码放在清单中声明的​​启动器活动的onResume()回调中。您可以将启动器活动设置为仅执行每次激活一次初始化的瘦活动,然后启动实际的主活动。

当然,可以运行先前的代码,例如onCreate()的{​​{1}}和Application的{​​{1}},所以它并不总是第一件事运行,但每次从菜单启动时都会保证运行。

答案 4 :(得分:0)

您可以将以下代码用于Kotlin

override fun onTrimMemory(level: Int) {
    super.onTrimMemory(level)
    when (level) {
        ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN -> {
            /*
               Release any UI objects that currently hold memory.

               "release your UI resources" is actually about things like caches.
               You usually don't have to worry about managing views or UI components because the OS
               already does that, and that's why there are all those callbacks for creating, starting,
               pausing, stopping and destroying an activity.
               The user interface has moved to the background.
            */
            System.exit(0);
        }
        ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE,
        ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW,
        ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL -> {
            /*
               Release any memory that your app doesn't need to run.

               The device is running low on memory while the app is running.
               The event raised indicates the severity of the memory-related event.
               If the event is TRIM_MEMORY_RUNNING_CRITICAL, then the system will
               begin killing background processes.
            */
        }
        ComponentCallbacks2.TRIM_MEMORY_BACKGROUND,
        ComponentCallbacks2.TRIM_MEMORY_MODERATE,
        ComponentCallbacks2.TRIM_MEMORY_COMPLETE -> {
            /*
               Release as much memory as the process can.
               The app is on the LRU list and the system is running low on memory.
               The event raised indicates where the app sits within the LRU list.
               If the event is TRIM_MEMORY_COMPLETE, the process will be one of
               the first to be terminated.
            */
        }

    }
}

还有下面的Java代码:

public void onTrimMemory(int level) {

        // Determine which lifecycle or system event was raised.
        switch (level) {

            case ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN:

                /*
                   Release any UI objects that currently hold memory.

                   "release your UI resources" is actually about things like caches. 
                   You usually don't have to worry about managing views or UI components because the OS 
                   already does that, and that's why there are all those callbacks for creating, starting, 
                   pausing, stopping and destroying an activity.
                   The user interface has moved to the background.
                */
                System.exit(0);
                break;

            case ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE:
            case ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW:
            case ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL:

                /*
                   Release any memory that your app doesn't need to run.

                   The device is running low on memory while the app is running.
                   The event raised indicates the severity of the memory-related event.
                   If the event is TRIM_MEMORY_RUNNING_CRITICAL, then the system will
                   begin killing background processes.
                */

                break;

            case ComponentCallbacks2.TRIM_MEMORY_BACKGROUND:
            case ComponentCallbacks2.TRIM_MEMORY_MODERATE:
            case ComponentCallbacks2.TRIM_MEMORY_COMPLETE:

                /*
                   Release as much memory as the process can.
                   The app is on the LRU list and the system is running low on memory.
                   The event raised indicates where the app sits within the LRU list.
                   If the event is TRIM_MEMORY_COMPLETE, the process will be one of
                   the first to be terminated.
                */

                break;

            default:
                /*
                  Release any non-critical data structures.
                  The app received an unrecognized memory level value
                  from the system. Treat this as a generic low-memory message.
                */
                break;
        }
    }

答案 5 :(得分:-1)

即使我关闭应用程序,在我的手机上也会继续在后台运行,直到您手动关闭它为止。

您可以使用

// Initialise the empty array
$courseCnt = array();

// Create location if not in array
if( ! array_key_exists($course['location'], $courseCnt)) { 
  $courseCnt[$course['location']] = array();
}

// Either increment the instructor or create with initial value of 1
if ( array_key_exists($course['instructor'], $courseCnt[$course['location']]) ) {
  $courseCnt[$course['location']][$courseCnt[$course['instructor']]] += 1;
}
else
{
  $courseCnt[$course['location']][$courseCnt[$course['instructor']]] = 1;
}

每次再次调用Activity时运行一些东西。

答案 6 :(得分:-2)

你应该在AndroidManifest.xml中的tag" application"设置字段android:name =" .ApplicationDemo"

<强> UPD 作者用我的断言编辑了问题。