【问题标题】:Is there a way to tell whether an activity is starting?有没有办法判断活动是否开始?
【发布时间】:2012-08-08 16:32:08
【问题描述】:

类似于 Activity 的 isFinishing() API 在 onPause() 中使用,是否有一个好的机制来确定在 onResume() 时是否正在创建 Activity?

【问题讨论】:

  • 嗯...活动在onCreate() 中创建,并在onStart() 中启动。您还需要哪些机制来确定何时创建和启动活动? ...或者我没有正确理解这个问题。
  • 我只想在返回活动时执行任务,例如填充列表,而不是在第一次创建活动时。
  • 现在我明白了。那么下面描述的带有标志的解决方案应该可以正常工作。
  • 这似乎是一个类似的问题。看看这个:*.com/questions/11842051/…

标签: android android-activity


【解决方案1】:

你可以有一个布尔变量来保存“isStarting”的状态。

boolean isStarting;

在您的 onCreate 方法中,您将其设置为 true:

isStarting = true;

所以在你的 onResume() 方法中你可以检查活动是否开始:

    if(isStarting == true) {
    // Activity has been created!
//set the variable to false
isStarting = false;
    }
    else {
    // Nope...
    }

我希望这会有所帮助!

【讨论】:

    【解决方案2】:

    您可以创建一个布尔值,最初为 false,您的 onResume 应该如下所示:

    if (!flag)
    {
        // Activity is created for the first time
        flag = true;
    }
    else
    {
        // Activity was created before
    }
    

    【讨论】:

      最近更新 更多