簡單分析AppDelegate生命週期
應用程序都有生命週期管理。比如Android應用程序的OnStart, OnStop, OnCreate等週期方法。同樣,Cocos2D-X 也有它的生命週期。
下面通過代碼分析下它的生命週期。
定義一個類 LifeCircleLogger.
// LifeCircleLogger.h
using namespace std;
class LifeCircleLogger
{
string m_msg;
public:
LifeCircleLogger();
~LifeCircleLogger();
LifeCircleLogger(const string& msg);
};
#define LOG_FUNCTION_LIFE LifeCircleLogger(__FUNCTION__)
// LifeCircleLogger.cpp
#include "LifeCircleLogger.h"
LifeCircleLogger::LifeCircleLogger()
{
}
LifeCircleLogger::LifeCircleLogger(const string&
msg): m_msg(msg)
{
cocos2d::CCLog( "%s BEGINS!" ,
m_msg.c_str());
}
LifeCircleLogger::~LifeCircleLogger()
{
cocos2d::CCLog( "%s ENDS!" ,
m_msg.c_str());
}
然後進入AppDelegate.cpp, 在生命週期方法:
AppDelegate::AppDelegate()
AppDelegate::~AppDelegate()
bool AppDelegate::applicationDidFinishLaunching()
void AppDelegate::applicationDidEnterBackground()
void AppDelegate::applicationWillEnterForeground()
添加宏 LOG_FUNCTION_LIFE
運行程序後我們就可以在控制檯輸出中得到Cocos2d-x的生命週期運行原理了。[ 如上圖所示 ]
OK, 這裏的生命週期分析對於其他應用也是同樣有效的,算一個小工具把。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.