Activity停止(但未销毁)时调用的onCreate().仅在安装后 [英] onCreate() called while Activity is stopped (but not destroyed). Only after installation

查看:163
本文介绍了Activity停止(但未销毁)时调用的onCreate().仅在安装后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个针对Api 19的应用程序,这基本上是一个处理草图.

I'm working on an app that targets Api 19, and that is basically a Processing sketch.

我面临的问题是,首次运行我的应用程序后,即安装该应用程序后,该应用程序运行良好,直到用户将其发送到后台为止.然后,如果他们再次单击应用程序图标,则会调用onCreate(),但该活动不会被破坏或重新启动.一些变量会发生变化,从而产生奇怪的行为.

The problem I'm facing is that the first time my app is run, right after installing it, it works well until the user sends it to the background. Then, if they click on the app icon again, onCreate() is called but the activity is not destroyed or restarted. Some variables change and that produces strange behaviours.

仅在首次使用该应用程序时发生.强制关闭它之后,就不会再发生这种行为了(据我测试).而且从Eclipse启动应用程序时也不会发生这种情况.

This happens ONLY the first time the app is used. After force closing it, this behaviour won't happen ever again (as far as I've tested). And this does not happen when launching the app from Eclipse either.

总结一下,这是在第一次力关闭后发生的情况(我认为是正确的):

Summarising, this is what happens after the first force close (and what I deem correct):

  • 活动正在运行.

  • Activity is running.

活动通过主页"按钮发送回去

Activity is sent to back via home button

onPause()

我们再次点击应用程序图标

We click on the app icon again

onResume()

onResume()

这是发生的-仅在安装后首次运行该应用程序时

And this is what happens -ONLY- the first time the app is run after installation:

  • 活动正在运行.

  • Activity is running.

活动通过主页"按钮发送回去

Activity is sent to back via home button

onPause()

我们再次点击应用程序图标

We click on the app icon again

onCreate()<-!!注意没有onDestroy()

onCreate() <-- !! note no onDestroy()

onResume()

onResume()

我想知道我使用沉浸模式是否与此有关,但是将Api目标版本更改为10,删除沉浸模式或在旧设备上进行测试并没有帮助.我当然在清单上使用了 android:configChanges ="orientation | keyboardHidden | screenSize" .

I wonder if the fact that I'm using Immersive Mode has anything to do with this, but changing the Api target version to 10, removing Immersive mode or testing on old devices do not help. I have, of course, used android:configChanges="orientation|keyboardHidden|screenSize" on my manifest.

有人知道这可能是什么原因吗?这是常见问题还是我应该在代码中寻找错误?也许是处理错误?

Does anyone have a clue on what might be causing this? Is this a common issue or should I look for a bug in my code? Maybe a Processing bug?

请先感谢您提供任何线索.我希望这是询问此问题的正确方法.这是我的第一篇文章.

Thanks in advance for any clue. I hope this is the right way to ask about this issue. This is my first post.

更新: 我的解释不是很准确,但是显然有一个错误报告.问题在这里得到了更好的解释: https://code.google.com /p/android/issues/detail?id = 26658

Update: My explanation is not very accurate, but apparently there is a bug report for this. The problem is much better explained here: https://code.google.com/p/android/issues/detail?id=26658

不幸的是,我无法使用建议的解决方案,在onCreate()中使用它会导致我的应用关闭或崩溃:

Unfortunately, I can't get the proposed solutions to work, using this in onCreate() causes my app to either close or crash:

if (!isTaskRoot()) {
  finish();
  return;
} 

推荐答案

好,如果有人撞到这堵墙,这就是我解决的方法.

Ok, this is how I solved it, in case anyone else bumps into this wall.

这可能会特别影响来自处理开发环境的人员,因为它将处理草图"转换为项目的唯一活动.

This might affect especially people coming from the Processing Developing Environment, as it converts a "Processing sketch" into the only activity of the project.

原始问题(Android从Package Installer启动应用程序时以不同的错误方式管理应用程序)在以下位置得到了很好的解释:

The original problem (Android managing apps in a different -wrong?- way when launching them from the Package Installer) is well explained here: https://code.google.com/p/android/issues/detail?id=26658

那里发布的解决方案可能会解决大多数情况,但是,如果像我一样,您的启动器活动是执行所有工作的活动,则您将需要创建一个特定的启动器活动,该活动仅启动主要活动...并提交Android错误发生时自杀.

Solutions posted there might solve most cases,but if -like me- your launcher activity is the one that carries out all the work, you will need to create a specific launcher activity that just starts the main one... and commits suicide when the Android bug happens.

将此位添加到启动器活动的onCreate()方法中:

Add this bit to the onCreate() method of the launcher activity:

if (!isTaskRoot()) {
 finish();
 return;
} 

我希望这会有所帮助.

这篇关于Activity停止(但未销毁)时调用的onCreate().仅在安装后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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