Android Study —— Activities

来源:互联网 发布:淘宝添加不了购物车 编辑:程序博客网 时间:2024/06/07 02:35

        What is activities?

        It is an application component that provides a screen with which users can interact in order to do something.

        我理解的activity就是手机程序里面的每一个界面,每一个界面就相当于是一个activity,在每一个activity中我们可以实现一定的操作,同时,也可以通过多种方式实现各个activity之间的相互调用。当然,在打开某一个程序的时候会首选启动一个activity,就类似于我们写C语言程序的时候会有一个main函数,程序都是从这里开始执行的。再换一个比方,就类似于我们打开一个网站,首页就对应了这个起始的activity,其他每一个页面都是一个activity,他们之间可以有数据的传送,可是分别执行不同的操作。

 

        Creating an Activity

        To create an activity, you must create a subclass of activity(or an existing subclass of it). In your subclass, you need to implement callback methods that the system calls when the activity transitions between various states of its lifecycle, such as when the activity is being created, stopped, resumed, or destroyed. The two most important callback methods are: onCreate() and onPause()

        在建立activity的时候,我们首先要建立activity的子类,在子类中要重写一些回调的方法。其中最重要的方法有两个:onCreate 和 onPause。我觉得onCreate比另一个还要重要,因为这个是在该activity加载生成的时候首先要执行的方法,我们对一些控件的绑定操作、监听事件等都是在此处完成的。在创建完新的activity之后,还需要在manifest这个文件里面“注册声明”一下,这样才可以正常使用。

<manifest ... >  <application ... >      <activity android:name=".ExampleActivity" />      ...  </application ... >  ...</manifest >

 

        Starting an Activity

        You can start another activity by calling startActivity(), passing it an Intent that describes the activity you want to start.

        Sometimes, you might want to receive a result from the activity that you start. In that case, start the activity by calling startActivityForResult() (instead of startActivity()). To then receive the result from the subsequent activity, implement the onActivityResult() callback method. When the subsequent activity is done, it returns a result in an Intent to your onActivityResult() method.

        可以有两种方法从一个activity开启另一个activity:startActivity() 和 startActivityForResult()。前一种方法就是单纯的开启另一个activity,当然有的时候我们需要所开启的activity去完成一些操作,然后把操作的结果再传回,这样就使用到了第二种开启方法startActivityForResult()。使用这种方法,只要在调用activity内重写onActivityResult(int requestCode, int resultCode,Intent data)方法就可以获得被调activity返回的数据了。此外这两种方法都可以从调用activity向被调activity传递数据,只需要使用Intent就好了。

 

        Shutting Down an Activity

        You can shut down an activity by calling its finish() method. You can also shut down a separate activity that you previously started by calling finishActivity().

        可以使用finish()方法去结束一个activity。

 

        Managing the Activity Lifecycle

       

 

 

原创粉丝点击