简体   繁体   English

了解android活动生命周期

[英]Understanding android activity lifecycle

I have a problem with the lifecycle of my android activity. 我的android活动的生命周期有问题。 When I press the button "home", the function onDestroy() is calling. 当我按下按钮“ home”时,函数onDestroy()正在调用。 I have test with a simple hello world and Toast on callback function. 我测试了一个简单的hello world和Toast的回调函数。

My code : 我的代码:

    public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
Toast.makeText(getApplicationContext(), "onCreate", Toast.LENGTH_SHORT).show();
}

    @Override
    protected void onRestart(){
        super.onRestart();
Toast.makeText(getApplicationContext(), "onRestart", Toast.LENGTH_SHORT).show();
    }

    @Override 
    protected void onStart(){
        super.onStart();
Toast.makeText(getApplicationContext(), "onStart", Toast.LENGTH_SHORT).show();
    }

    @Override 
    protected void onResume(){
        super.onResume();
Toast.makeText(getApplicationContext(), "onResume", Toast.LENGTH_SHORT).show();
    }
    @Override 
    protected void onStop(){
        super.onStop();
        Toast.makeText(getApplicationContext(), "onStop", Toast.LENGTH_SHORT).show();
    }
    @Override
    protected void onDestroy(){
    super.onDestroy();
Toast.makeText(getApplicationContext(), "onDestroy", Toast.LENGTH_SHORT).show();
}
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

EDIT : When I run my application, I have the toast "onCreate", "onState" then "onResume. If I press "home" buton I see the toast "onStop". If I return on my application I have the toast "onStart" and "onResume". But when I run my application on my real device (Samsung GT-P3110), I have the toast "onCreate", "onState" then "onResume. 编辑:当我运行我的应用程序时,我有祝酒词“ onCreate”,“ onState”然后是“ onResume”。如果我按“ home”按钮,但是我看到了祝酒词“ onStop”。如果我在我的应用程序上返回,则祝酒词“ onStart”但是,当我在真实设备(三星GT-P3110)上运行我的应用程序时,我会敬酒“ onCreate”,“ onState”然后“ onResume”。 If I press "home" buton I see the toast "onStop" and "onDestroy". 如果我按“ home”按钮,则会看到吐司“ onStop”和“ onDestroy”。 (My application is alway visible on the list of running application) If I return on the application, it see "onCreate" -> onState" -> "onResume" like it completly restart. (我的应用程序始终在正在运行的应用程序列表上可见)如果我返回该应用程序,它会像“完全创建”一样看到“ onCreate”-> onState”->“ onResume”。

I don't understand. 我不明白 Can you enlighten me please ? 你能启发我吗?

An activity represents the screen you see on your phone when app is running, if this screen is gone then your activity is Destroyed. 活动代表您在应用程序运行时在手机上看到的屏幕,如果该屏幕消失了,那么您的活动将被销毁。

see this tutorial for info: 有关信息,请参见本教程:

http://developer.android.com/training/basics/activity-lifecycle/starting.html http://developer.android.com/training/basics/activity-lifecycle/starting.html

you can read more about this and run the activity apk sample, it will help you alot understanding how it works. 您可以阅读有关此内容的更多信息并运行活动apk示例,它将帮助您充分了解其工作原理。 (click on Download The Demo and run it somewhere, in a VM or on your phone) (单击“下载演示”,然后在虚拟机或手机上的某个位置运行)

Your activity is actually destroyed when it is stopped, an excerpt from here : 您的活动停止时实际上已被破坏,摘录自此处

Note: The system calls onDestroy() after it has already called onPause() and onStop() in all situations except one: 注意:在所有情况下,系统在已经调用过onPause()和onStop()之后都会调用onDestroy():

the 'except one' in this instance does not apply to you, and it is only when finish() is called from the onCreate() method. 在这种情况下,“除外”不适用于您,仅当从onCreate()方法调用finish()时才适用。 The rest of the excerpt is on the bottom of the page linked above. 摘录的其余部分位于上面链接的页面的底部。

In addition of the comments below, you can make the distinction between the BACK and the HOME button with the method : 除了下面的注释,您还可以使用方法来区分BACK和HOME按钮:

void onSaveInstanceState(Bundle outState)

called when you press the HOME button but not the BACK button. 当您按下HOME按钮而不是BACK按钮时调用。

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

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