以下资料来自Android文档
An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View). While activities are often presented to the user as full-screen windows, they can also be used in other ways: as floating windows (via a theme with windowIsFloating set) or embedded inside of another activity (using ActivityGroup).
在我理解看来,Activity就是一个容器,它里面可以放置各种UI控件,也可以互相包含,嵌套或调用。
上图就是Activity的生命周期。
Activity运行时会先后调用onCreate(),onStart()和onResume()。
当Activity被其他Activity遮盖时就会调用onPause(),不可见时调用onStop()。
Activity内调用finish()结束或被操作系统销毁时会调用onDestroy()。
在onPause()时,用户返回了Activity,就会调用onResume(),
在onStop()时,用户回到Activity中,就会调用onRestart()。
上图就说的非常清楚。
写了一个例子,里面有Activityone和Activitytwo两个Activity,
Activityone调用Activitytwo,两个里面都重写了上述方法,里面System.out.println出信息用来区分调用了谁。
输出的信息如下:
(程序开始,Activityone创建)
Activity one—————onCreate
Activity one—————onStart
Activity one—————onResume
(调用Activitytwo)
Activity one—————onPause
Activity two—————onCreate
Activity two—————onStart
Activity two—————onResume
Activity one—————onStop
(按返回键)
Activity two—————onPause
Activity one—————onRestart
Activity one—————onStart
Activity one—————onResume
Activity two—————onStop
Activity two—————onDestroy
(退出程序)
Activity one—————onPause
Activity one—————onStart
Activity one—————onDestroy