第5章 Android基本程序单元Activity

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

3G应用开发之AndroidActivity学习目标何谓回调Activity简介Activity的生命周期何谓回调前一节中我们介绍了事件处理机制,这里面就有方法的回调。C,C++或者javascript中经常有回调,但他们是通过指针来实现的,java不能直接操作指针,java中的回调是通过接口来实现的。方法回调是功能定义和功能实现的一种手段,是一种松藕合设计思想。作为一种系统架构,必须有自己的运行环境,并提供用户的实现接口。例如:在activity中定义了很多生命周期中不同状态要调用的方法,这些方法都是空实现,系统框架要调用,用户也要调用来实现。Activity简介Activity的创建启动另一个ActivityActivity之间传递数据启动另外一个activity并返回结果Activity的创建Activity提供了和用户交互的可视化界面。创建一个Activity一般是继承Activity(当然也可以继ListActivity,MapActivity),覆盖activity的onCreate()方法,在该方法中调用setContentView()方法展示要显示的视图,调用findViewById()方法实例化组件。注Activity只有在清单文件(全局配置文件)中声明才能使用。启动另一个Activity像我们在做web开发时,经常会从一个页面跳转到另一个页面,在android中我们也经常会从一个activity中跳转到另一个activity做出一些处理。从一个activity启动另一个activity可以使用startActivity()方法或者startActivityForResult()方法(能够返回结果)。这两个方法要传递的参数是android中的另一个非常重要的组件Intent,Intent是相同或者不同组件的信使。Activity之间传递数据在web开发中我们经常把数据放在某个Scope中(如:request,session)来实现数据共享。在activity之间传递数据,要使用到对象Bundle,我们将要传递的信息封装到该对象里面,并通过Intent对象传递到另一个Intent中。Activity_03启动另一个activity并返回结果我们讲述了如何启动另一个activity,有时候我们需要启动另一个activity,并返回一个结果。这里我们就需要使用另一个方法startActivityForResult()。1.覆盖onActivityResult()方法。从另一个activity中获得返回的结果。2.通过使用startActivityForResult()方法传递一个Intent到另一个activity中。3.在另一个activity中通过getIntent()方法获得Intent,通过setResult(0,intent)方法返回结果(intent)。Chapter05_Activity_StartActivityForResultActivityLifecycleIfanactivityintheforegroundofthescreen(atthetopofthestack),itisactiveorrunning.Ifanactivityhaslostfocusbutisstillvisible(thatis,anewnon-full-sizedortransparentactivityhasfocusontopofyouractivity),itispaused.Apausedactivityiscompletelyalive(itmaintainsallstateandmemberinformationandremainsattachedtothewindowmanager),butcanbekilledbythesysteminextremelowmemorysituations.ActivityLifecycleIfanactivityiscompletelyobscuredbyanotheractivity,itisstopped.Itstillretainsallstateandmemberinformation,however,itisnolongervisibletotheusersoitswindowishiddenanditwilloftenbekilledbythesystemwhenmemoryisneededelsewhere.Ifanactivityispausedorstopped,thesystemcandroptheactivityfrommemorybyeitheraskingittofinish,orsimplykillingitsprocess.Whenitisdisplayedagaintotheuser,itmustbecompletelyrestartedandrestoredtoitspreviousstate.ActivityLifecycleTheentirelifetimeofanactivityhappensbetweenthefirstcalltoonCreate(Bundle)throughtoasinglefinalcalltoonDestroy().AnactivitywilldoallsetupofglobalstateinonCreate(),andreleaseallremainingresourcesinonDestroy().Forexample,ifithasathreadrunninginthebackgroundtodownloaddatafromthenetwork,itmaycreatethatthreadinonCreate()andthenstopthethreadinonDestroy().ActivityLifecycleThevisiblelifetimeofanactivityhappensbetweenacalltoonStart()untilacorrespondingcalltoonStop().Duringthistimetheusercanseetheactivityon-screen,thoughitmaynotbeintheforegroundandinteractingwiththeuser.Betweenthesetwomethodsyoucanmaintainresourcesthatareneededtoshowtheactivitytotheuser.Forexample,youcanregisteraBroadcastReceiverinonStart()tomonitorforchangesthatimpactyourUI,andunregisteritinonStop()whentheuserannolongerseewhatyouaredisplaying.TheonStart()andonStop()methodscanbecalledmultipletimes,astheactivitybecomesvisibleandhiddentotheuser.ActivityLifecycleTheforegroundlifetimeofanactivityhappensbetweenacalltoonResume()untilacorrespondingcalltoonPause().Duringthistimetheactivityisinfrontofallotheractivitiesandinteractingwiththeuser.Anactivitycanfrequentlygobetweentheresumedandpausedstates--forexamplewhenthedevicegoestosleep,whenanactivityresultisdelivered,whenanewintentisdelivered--sothecodeinthesemethodsshouldbefairlylightweight.Activity生命周期主要有4种状态:Active(活动)Paused(暂停)Stopped(停止)Dead(死亡)活动、前景状态暂停、背景状态Activity生命周期其生命周期涉及的函数有:voidonCreate(BundlesavedInstanceState)voidonStart()voidonRestart()voidonResume()voidonPause()voidonStop()voidonDestroy()Activity_04,05,06

1 / 17
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功