Java 在Android应用程序中确定onCreate()与onRestartingFromBackGround()的对比

Java 在Android应用程序中确定onCreate()与onRestartingFromBackGround()的对比,java,android,oncreate,Java,Android,Oncreate,我正在设计的Android应用程序有一个小问题 每当这两个事件发生时,我需要运行一些代码: 1. The app is NOT running in the background, so the user launches it. 2. The app IS already running in the background, so the user is really just re-opening it. (I only need to run the code once, n

我正在设计的Android应用程序有一个小问题

每当这两个事件发生时,我需要运行一些代码:

1.  The app is NOT running in the background, so the user launches it.
2.  The app IS already running in the background, so the user is really 
    just re-opening it.
(I only need to run the code once, not twice.)
无论我将代码调用放在何处(onCreate、onStart、onRestart、onResume等),我总是会产生不希望的影响:

A. My code gets run twice when #2 happens.
B. My code runs even when the user is just moving from 
   MAIN to a SUB-ACTIVITY, then back to MAIN again.
C. My code doesn't run at all.
我是否可以对onCreate()和onRestartingFromBackGround()进行区分


我原以为我可以使用onRestart(),但我很惊讶地看到onRestart()即使在我这样做的时候也能运行。#B(#B真的被认为是我的应用程序的“重启”吗??)

从纯java的角度来看,你可以在第一次按下图标时使用加载线程。此加载线程可以轮询手机,查看主活动线程当前是否正在运行,然后从加载线程移动到正确的代码段。对于ANDROID,我认为,您将轮询进程名称或进程ID…任何人都曾轮询过操作系统的进程???

“(我只需要运行代码一次,而不是两次。)”——根据定义,它必须运行两次#2表示#1以前发生过,否则应用程序将不在后台。您可以尝试检查的值,看看这是否允许您根据情况控制希望发生的事情。您可以在
onStop()
finish()
,然后将代码放入
onCreate
“根据定义,它必须运行两次。”嗯?所以我不能控制我做什么?即使我不想,我也得运行两次?(修复#A很简单…带有bool标志。)但这会导致我列出的其他问题。getIntent()已被弃用,因此我不希望用旧的、弃用的代码加载我的应用程序。看起来getIntent()未被弃用。。。但我确实不知道如何使用它来检测“首次运行”或“从后台重新运行”。有人有代码示例吗?