Activity创建过程:
1.建立Activity类及定义属性及内部方法;
2.注册Activity到manifast中;
3.在启动中使用onCreat()实现业务:
(1)界面定义
(2)setContentView()加载页面;
//将layout压缩成View View view= this .getLayoutInflater().inflate(R.layout.activity_activity_demo, null ); |
Intent:
Intent有两种:Explicit intents(明确的意图);Implicit intents (含蓄的意图)
Explicit intents:通过组件名启动
Implicit intents:通过匹配manifest file中的Intent filter来启动组件,若匹配结果仅有一项,直接启动;若多项匹配,根据弹出的dialog进行选择
Intent属性:
1.Component:启动Explicit intents必不缺少的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 |
//显示启动Intent,必须指定Intent属性Component,Component属性需要指定ComponentName //1.直接创建时指定 Intent intent= new
Intent(ActivityDemoActivity. this , ActivityIntentDemo. class ); Intent intent= new
Intent(); //2.通过ComponentName构造器来指定;只须指定包名与类名就可以唯一确定组件名 ComponentName componentName= new
ComponentName( "androidstudy.ActivityDemo" , "androidstudy.ActivityDemo.ActivityIntentDemo" ); ComponentName componentName= new
ComponentName(ActivityDemoActivity. this ,ActivityIntentDemo. class ); ComponentName componentName= new
ComponentName(ActivityDemoActivity. this , "androidstudy.ActivityDemo.ActivityIntentDemo" ); intent.setComponent(componentName); //3.setClass()与setClassName()指定 intent.setClass(ActivityDemoActivity. this , ActivityIntentDemo. class ); intent.setClassName(ActivityDemoActivity. this , "androidstudy.ActivityDemo.ActivityIntentDemo" ); intent.setClassName( "androidstudy.ActivityDemo" , "androidstudy.ActivityDemo.ActivityIntentDemo" ); startActivity(intent); |
返回Intent传递参数是否成功
调用startActivityForResult(intent, 1234);
intent是传递的Intent,1234为RequestResult,成功后回调函数protected void onActivityResult(int requestCode, int resultCode, Intent data) ;
在新的Activity 中通过调用函数setResult(5678,mIntent);设置回调的resultCode与Intent
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 |
//调用Intent的Activity public
void
demo(View view){ //显示启动Intent,必须指定Intent属性Component,Component属性需要指定ComponentName //1.直接创建时指定 //Intent intent=new Intent(ActivityDemoActivity.this, ActivityIntentDemo.class); intent= new
Intent(); //2.通过ComponentName构造器来指定;只须指定包名与类名就可以唯一确定组件名 //ComponentName componentName=new ComponentName("androidstudy.ActivityDemo","androidstudy.ActivityDemo.ActivityIntentDemo"); //ComponentName componentName=new ComponentName(ActivityDemoActivity.this,ActivityIntentDemo.class); //ComponentName componentName=new ComponentName(ActivityDemoActivity.this,"androidstudy.ActivityDemo.ActivityIntentDemo"); //intent.setComponent(componentName); //3.setClass()与setClassName()指定 intent.setClass(ActivityDemoActivity. this , ActivityIntentDemo. class ); //intent.setClassName(ActivityDemoActivity.this, "androidstudy.ActivityDemo.ActivityIntentDemo"); //intent.setClassName("androidstudy.ActivityDemo","androidstudy.ActivityDemo.ActivityIntentDemo"); intent.putExtra( "STUDY" , "hello 阳光 " ); startActivityForResult(intent, 1234 ); } protected
void
onActivityResult( int
requestCode, int
resultCode, Intent data) { if (resultCode== 5678 ){ Toast.makeText(ActivityDemoActivity. this , data.getExtras().getString( "STUDY" ), Toast.LENGTH_LONG).show(); System.out.println(intent.getStringExtra( "STUDY" )); } super .onActivityResult(requestCode, resultCode, data); }; //接收Intent的Activity public
void
click(View view){ mIntent.putExtra( "STUDY" , "hello moon" ); //设置返回值 setResult( 5678 ,mIntent); //关闭此Activity finish(); } |
原文:http://www.cnblogs.com/Eudora/p/3513663.html