Android的活性生命周期功能基本 [英] basics of android activity life cycle functions

查看:224
本文介绍了Android的活性生命周期功能基本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我测试了这code,显示其状态的活动是

I was testing out this code which shows which state an activity is in

public class Activity101Activity extends Activity {
    String tag  =  "Lifecycle";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
        // setContentView(R.layout.main);
        setContentView(R.layout.activity_activity101);
        Log.d(tag , "In the onCreate() event");
    }
    public void onStart()
    {
        super.onStart();
        Log.d(tag , "In the onStart() event");
    }

    public void onRestart()
    {
         super.onRestart();
        Log.d(tag , "In the onRestart() event");
    }

    public void onResume()
    {
         super.onResume();
        Log.d(tag , "In the onResume() event");
    }

    public void onPause()
    {
         super.onPause();
        Log.d(tag , "In the onPause() event");
    }

    public void onStop()
    {
         super.onStop();
        Log.d(tag , "In the onStop() event" );
    }

    public void onDestroy()
    {
         super.onDestroy();
        Log.d(tag , "In the onDestroy() event");
    }
}  

所以我看到的onDestroy()只调用的时候,我们preSS后退按钮,而该活动是在屏幕上,而不会被调用,否则。因此,它应该运行在回地面,如果我preSS home键的活动正在运行时。但是,如果我去设置 - >应用程序 - >运行我还看不出来就行了。这是否意味着它在后台运行或不?

so I see that onDestroy() is only called when we press the back button while the activity is on screen, and is never called otherwise. So it should be running in the back ground if I press the home button while the activity is running. However, if I go to Settings -> Apps -> Running I can't see it on the list. So does that mean it is running in the background or not?

此外,同样,这code表示的onPause()总是跟着的onStop()和ONSTART()总是跟着onResume()。那么,为什么把它们定义为不同的功能在Android环境中,并且不合并?

Again, Again, this code shows that onPause() is always followed by onStop() and onStart() is always followed by onResume(). So why are they defined as different functions in the Android environment and not combined?

推荐答案

在活动进去backstack,它是在挂起模式。所以你不要看它在运行的应用程序列表。一旦你重新启动这种暂停的应用程序,它来自backstack到前台,并开始运行。它被保存在backstack至preserve其状态,并继续从之前去的背景下得到了停下的地方。

Once the activity goes in backstack, it is in suspended mode. So you dont see it in running app list. Once you relaunch such suspended application, it comes to foreground from backstack and starts to run. It is kept in backstack to preserve its state and resume from the place where it got stopped before going in background.

要了解原因,ONSTART是onResume跟随下面的链接之前需要。这将清除所有的疑虑很清楚:

To understand why, onStart is needed before onResume follow the link below. It will clear all your doubts very clearly:

ONSTART()和onResume之间的差异()

这篇关于Android的活性生命周期功能基本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆