
一般来说。熟悉AndroID程序设计的人都知道AndroID有三个基础组件Activity,Service和broadcastReceiver,他们都是依赖Intent来启动。本文所要介绍的是Activity的生命周期以及针对Activity的Intent使用。
之前的例子一直都是使用Activity,在一个Layout XML与一个Activity捆绑的情况下可以视为一个Form,多个Layout XML与一个Activity捆绑的话那就是个Application本身了。Intent可以分为显式Intent和隐式Intent:显式Intent用于启动明确的目标组件(前面所说的三大组件),同一个Application内的多个Activity调用也是显式Intent;隐式Intent就是调用没有明确的目标组件,可以是系统也可以是第三方程序。隐式Intent一般用于调用系统组件功能,相关例程都是网络上很容易找到的(调用某些系统组件的时候要申请权限)。
Acitivity的运行状况分为:onCreate、onDestroy、onStart、onStop、onRestart、onResume、onPause,onCreate对应onDestroy,onStart对应onStop,onResume对应onPause。
先贴出本文运行截图如下:
这个是从Acitivity1转到Activity2的时候,Acitivity1的状态变化,使用了finish()会触发onDestroy()。
这个是从Activity2转到Activity1的时候,Acitivity2的状态变化。从两次Activity的启动可以看出,流程是onCreate()->onStart()->onResume()三个方法,销毁是onPause()->onStop()->onDestroy()。另外,要往工程添加第二个Activity,需要到AndroIDManifest.xml->Application那里添加Activity2。
main1.xml的源码如下:
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:orIEntation="vertical" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent"> <button androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:ID="@+ID/main1.button01" androID:text="跳转到Activity2"></button> <EditText androID:text="@+ID/EditText01" androID:ID="@+ID/EditText01" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content"></EditText> <button androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:ID="@+ID/main1.button02" androID:text="跳转到外部Activity"></button></linearLayout>
main2.xml的源码如下:
<?xml version="1.0" enCoding="UTF-8"?><linearLayout androID:ID="@+ID/linearLayout01" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" xmlns:androID="http://schemas.androID.com/apk/res/androID"> <button androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:ID="@+ID/main2.button01" androID:text="返回Activity1"></button></linearLayout>
Activity1的Java源码如下:
package com.testActivityIntent;import androID.app.Activity;import androID.content.Intent;import androID.content.SharedPreferences;import androID.net.Uri;import androID.os.Bundle;import androID.util.Log;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.EditText;public class testActivityIntent extends Activity { /** Called when the activity is first created. */ button btnToInternalActivity; button btnToExternalActivity; EditText tbBundle; @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.e("Activity1","onCreate");//显示当前状态,onCreate与onDestroy对应 setContentVIEw(R.layout.main1); btnToInternalActivity=(button)this.findVIEwByID(R.ID.main1_button01); btnToExternalActivity=(button)this.findVIEwByID(R.ID.main1_button02); btnToInternalActivity.setonClickListener(new ClickEvent()); btnToExternalActivity.setonClickListener(new ClickEvent()); tbBundle=(EditText)this.findVIEwByID(R.ID.EditText01); } public voID onDestroy() { super.onDestroy(); Log.e("Activity1","onDestroy");//显示当前状态,onCreate与onDestroy对应 } @OverrIDe public voID onStart() { super.onStart(); Log.e("Activity1","onStart");//显示当前状态,onStart与onStop对应 } @OverrIDe public voID onStop() { super.onStop(); Log.e("Activity1","onStop");//显示当前状态,onStart与onStop对应 } @OverrIDe public voID onRestart() { super.onRestart(); Log.e("Activity1","onRestart"); } @OverrIDe public voID onResume() { super.onResume(); Log.e("Activity1","onResume");//显示当前状态,onPause与onResume对应 SharedPreferences prefs = getPreferences(0); //SharedPreferences 用于存储数据 String restoredText = prefs.getString("editText01",null); if (restoredText != null) { this.tbBundle.setText(restoredText); } } @OverrIDe public voID onPause() { super.onResume(); Log.e("Activity1","onPause");//显示当前状态,onPause与onResume对应 //保存文本框的内容,使得重回本Acitivity的时候可以恢复 SharedPreferences.Editor editor = getPreferences(0).edit();//SharedPreferences 用于存储数据 editor.putString("editText01",this.tbBundle.getText().toString()); editor.commit(); } class ClickEvent implements VIEw.OnClickListener{ @OverrIDe public voID onClick(VIEw v) { if(v==btnToInternalActivity) { Intent intent = new Intent(); intent.setClass(testActivityIntent.this,Activity2.class); //new一个Bundle对象,并将要传递的数据传入 Bundle bundle = new Bundle(); bundle.putString("Text",tbBundle.getText().toString()); //将Bundle对象assign给Intent intent.putExtras(bundle); //调用Activity2 startActivity(intent); testActivityIntent.this.finish();//会触发onDestroy(); } else if(v==btnToExternalActivity) { //有些外部调用需要开启权限 Uri uri = Uri.parse("http://Google.com"); Intent it = new Intent(Intent.ACTION_VIEW,uri); startActivity(it); } } }}Activity2的Java源码如下:
package com.testActivityIntent;import androID.app.Activity;import androID.content.Intent;import androID.os.Bundle;import androID.util.Log;import androID.vIEw.VIEw;import androID.Widget.button;public class Activity2 extends Activity { button btnBackMain1; public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.e("Activity2","onCreate");//显示当前状态,onCreate与onDestroy对应 //加载activity2.xml setContentVIEw(R.layout.main2); //得Intent中的Bundle对象 Bundle bunde = this.getIntent().getExtras(); //取得Bundle对象中的数据 Log.i("In_Text",bunde.getString("Text")); btnBackMain1=(button)this.findVIEwByID(R.ID.main2_button01); btnBackMain1.setonClickListener(new ClickEvent()); } public voID onDestroy() { super.onDestroy(); Log.e("Activity2","onDestroy");//显示当前状态,onCreate与onDestroy对应 } @OverrIDe public voID onStart() { super.onStart(); Log.e("Activity2","onStart");//显示当前状态,onStart与onStop对应 } @OverrIDe public voID onStop() { super.onStop(); Log.e("Activity2","onStop");//显示当前状态,onStart与onStop对应 } @OverrIDe public voID onRestart() { super.onRestart(); Log.e("Activity2","onRestart"); } @OverrIDe public voID onResume() { super.onResume(); Log.e("Activity2","onResume");//显示当前状态,onPause与onResume对应 } @OverrIDe public voID onPause() { super.onResume(); Log.e("Activity2","onPause");//显示当前状态,onPause与onResume对应 } class ClickEvent implements VIEw.OnClickListener{ @OverrIDe public voID onClick(VIEw v) { if(v==btnBackMain1) { Intent intent = new Intent(); intent.setClass(Activity2.this,testActivityIntent.class); //调用Activity1 startActivity(intent); Activity2.this.finish();//会触发onDestroy(); } } }}希望本例所述的Acitivity用法能对大家的AndroID程序开发起到一定的帮助作用。
总结以上是内存溢出为你收集整理的Android提高之Activity+Intent用法示例全部内容,希望文章能够帮你解决Android提高之Activity+Intent用法示例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)