简体   繁体   English

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

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

Iam little bit amazed with this.I have an onResume() in my activity.Its called and works well in my emulator, but in a physical device samsung galaxy note for specific with jellybean installed,its not called.Instead onCreate() is called all the time.Why this happens? 我对此有点惊讶。我的活动中有一个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;
    }
}

This is my code.firsttime is a static boolean variable.It is used to prevent onResume() being called when app is started for the first time 这是我的代码。firsttime是一个静态布尔变量,用于防止在首次启动应用程序时调用onResume()

Try to print something inside the onResume and check it in LogCat.... the code inside onResume may be causing this. 尝试在onResume内打印某些内容,然后在LogCat中检查它。...onResume内的代码可能会导致这种情况。 or else can you elaborate your question? 否则您可以详细说明您的问题吗?

Considering your current scenario, you should save variable in preferences instead of relying on activities lifecycle since lifecycle depends on many things. 考虑到您当前的情况,由于生命周期取决于很多事情,因此您应该将变量保存在首选项中,而不要依赖活动生命周期。 Using static variable for this scenario is bad choice in general.I think this should solve your problem. 通常,在这种情况下使用静态变量是错误的选择。我认为这应该可以解决您的问题。

I think here is what happens, when your app not the Top app, the activity manager actually destroy the activity, it only called 我认为这是发生了什么,当您的应用程序不是顶级应用程序时,活动管理器实际上破坏了活动,它只调用了

    public void onSaveInstanceState(Bundle savedInstanceState)

no 没有

    onStop

called, so no 叫,所以没有

    noResume

will be called. 将被称为。

The correct to do this is, when put all states of this activity when 正确的做法是,当将该活动的所有状态置于

    public void onSaveInstanceState(Bundle savedInstanceState)

called. 叫。

and in your onCreate() function, do such thing 并在您的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
    }
    ...
}

to check if you have some saved state. 检查您是否有一些保存状态。

Most code was copy from android developer site: http://developer.android.com/training/basics/activity-lifecycle/recreating.html 大多数代码是从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