繁体   English   中英

在物理设备中未调用onResume(),而是调用了onCreate()

[英]onResume() is not called in physical device instead onCreate() is called

我对此有点惊讶。我的活动中有一个onResume()。它在模拟器中被调用并运行良好,但是在物理设备中安装了软糖的三星银河笔记中,它没有被调用,而是被称为onCreate()一直如此为什么会这样?

public void onResume(){
    super.onResume();
    if(firsttime){
        try {
            Toast.makeText(getApplicationContext(), "Resuming Activity",Toast.LENGTH_LONG).show();
            addReminder();
        } catch(Exception exception) {
            exception.printStackTrace();
        }
    } else {
        firsttime=true;
    }
}

这是我的代码。firsttime是一个静态布尔变量,用于防止在首次启动应用程序时调用onResume()

尝试在onResume内打印某些内容,然后在LogCat中检查它。...onResume内的代码可能会导致这种情况。 否则您可以详细说明您的问题吗?

考虑到您当前的情况,由于生命周期取决于很多事情,因此您应该将变量保存在首选项中,而不要依赖活动生命周期。 通常,在这种情况下使用静态变量是错误的选择。我认为这应该可以解决您的问题。

我认为这是发生了什么,当您的应用程序不是顶级应用程序时,活动管理器实际上破坏了活动,它只调用了

    public void onSaveInstanceState(Bundle savedInstanceState)

没有

    onStop

叫,所以没有

    noResume

将被称为。

正确的做法是,当将该活动的所有状态置于

    public void onSaveInstanceState(Bundle savedInstanceState)

叫。

并在您的onCreate()函数中执行此操作

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); // Always call the superclass first

    // Check whether we're recreating a previously destroyed instance
    if (savedInstanceState != null) {
        // Restore value of members from saved state
        mCurrentScore = savedInstanceState.getInt(STATE_SCORE);
        mCurrentLevel = savedInstanceState.getInt(STATE_LEVEL);
    } else {
        // Probably initialize members with default values for a new instance
    }
    ...
}

检查您是否有一些保存状态。

大多数代码是从android开发人员网站上复制的: http : //developer.android.com/training/basics/activity-lifecycle/recreating.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM