Activity简介:
用于处理用户操作,几乎所有的activity都要和用户打交道,所以activity 类通过setcontentView()方法将UI放到Avtivity创建的窗口中;
首次启动时:
onCreate--onStart-----onResume;
跳转到第二个Activity:
第一个activity onPause,第二个activity onCreate--onStart--onResume;然后第一个activity调用onStop;
第二个反回第一个
第二个activity onPause()--->onStop()->onDestory(),第一个activity onRestart()--->onStart()--->onResume()
各个生命周期的状态和使用情况
当第一次调用一个Activity就会执行onCreate方法
onCreate(Bundle)方法是初始化activity的地方. 在这儿通常可以调用setContentView(int)设置在资源文件中定义的UI, 使用findViewById(int) 可以获得UI中定义的窗口.
当Activity处于可见状态的时候就会调用onStart方法
当Activity可以得到用户焦点的时候就会调用onResume方法
当Activity没有被销毁的时候重新调用这个Activity就会调用onRestart方法
当Activity被遮挡住的时候就会调用onPause方法
onPause()接口是使用者准备离开activity的地方,在这儿,任何的修改都应该被提交(通常用于ContentProvider保存数据).
当Activity处于不可见状态的时候就会调用onStop方法
当Activity被销毁时会调用onDestory方法
当Activity没有被销毁的时候重新调用这个Activity就会调用onRestart方法
Intent传递
Intent传递数据不超过0.5M,否则抛出TransactionToolargeException异常
可传递基本数据类型,传递对象intent.putParcelableArrayListExtra("cx",ArrayList<? extends Parcelable> value) value需要的对象要实现Parcelable接口;也可通过bundle.putSerializable("df",Serializable);Serializable一个对象序列化的接口,一个类只有实现了Serializable接口,它的对象才是可序列化的。因此如果要序列化某些类的对象,这些类就必须实现Serializable接口。而实际上,Serializable是一个空接口,没有什么具体内容,它的目的只是简单的标识一个类的对象可以被序列化。
Bundel传递
Bundel相当于一个容器,可装各种数据类型。通过Intent.PutBundel("",Bundel bundel)方法传递数据,Intent和Bundel都是通过键值对方式传递;接收时通过
Bundle myBundelForGetName=this.getIntent().getExtras();
String name=myBundelForGetName.getString("Key_Name");
写入配置文件
Preference: SharedPreferences也是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值
对数据,通常用来存储一些简单的配置信息
SharedPreferences对象本身只能获取数据而不支持存储和修改,存储修改是通过Editor对象实现。实现
SharedPreferences存储的步骤如下:
一、根据Context获取SharedPreferences对象
二、利用edit()方法获取Editor对象。
三、通过Editor对象存储key-value键值对数据。
四、通过commit()方法提交数据。
SharedPreferences sharedPreferences = this.getSharedPreferences("name",0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("sd","dfd");
editor.commit();
Static共享
网络共享
Activity的两种启动方式
• 直接启动、
Intent intent = new Intent(上下文,目标Activity);
startActivity(intent);
通过创建Intent对象,调用startActivity(intent)方法传递一个Intent对象的方式启动;也可创建ComponentName对象
Componentneme name = new ComponentName(上下文,目标Activity);
Intent.setComponent(intent);
startActivity(intent);
2匿名启动
通过Manifesest文件指定Action,
<activity android:name=".B"
android:launchMode="standard">
<intent-filter>
<action android:name="android.intent.action.CALL"/>
<category android:name="android.intent.category.ALTERNATIVE"/>
</intent-filter>
Intent intent = new Intent();
Intent.tAction(intent.action.CALL);
Intent的属性
动作(Action),数据(Data),分类(Category),类型(Type),组件(Compent)以及扩展信(Extra)。其中最常用的是Action属性和Data属性。