例如,用户打开一个应用程序,按下主页按钮,然后回来到应用程序一次。
有没有什么办法来触发某些功能,当用户导航回应用程序? 如自动当用户返回到该应用程序加载的视图对象。
这个问题是两个Android和iOS。
例如,用户打开一个应用程序,按下主页按钮,然后回来到应用程序一次。
有没有什么办法来触发某些功能,当用户导航回应用程序? 如自动当用户返回到该应用程序加载的视图对象。
这个问题是两个Android和iOS。
使用以下到您的喜好在项目的AppDelegate.m
文件(仅iOS版)
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
在的onPause()方法编写代码来知道你的应用程序去背景。
public void onPause()
{
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
Boolean flag=false;
List<RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
if (!topActivity.getPackageName().equals(context.getPackageName())) {
flag=true;
}
}
if(flag)
{
//App went to background...
}
}
使用中的onResume()上面的标志 ,知道您的应用程序恢复。
为Android,你可以在当研究背景的应用程序来前即所谓的onResume功能权限代码。 但要记住的Android生命周期是
的onCreate - >的onResume。
这样的onResume总是被调用,即使应用程序运行的第一次还是来自背景。 但所谓的onCreate只有当活动创建第一次。
您可以设置的onPause方法的一些变量时,应用程序会为background,它被称为
当你拿到变量“真”和所谓的onResume可以执行你的任务。
请享用。
尝试在胡亚蓉,onKeyDown和的onPause的两种方法使用一个布尔变量。
boolean backFromBackground = false;
onCreate(){
//whatever you want
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_HOME)
{
backFromBackground = true;
}
}
onPause(){
if(backFromBackground){
//do what ever you want
}
}