活动,一个界面称为一个活动
如何显示这个活动:
res:文件夹下的一个文件夹layout(布局),右键新建一个xml文件
可以选择LinearLayout 线型的 和 RelativeLayout 相关型的 居多
具体要实现的布局具体分析
src:新建一个包,包名建议和AndroidManifest.xml文件里面package=”com.baozhong.xml”一样
新建包之后再新建一个MainActivity class文件继承Activity
其中MainActivity中这个方法不可少:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
}
最后在AndroidManifest.xml里面注册
一般是这样的:
<activity
android:name=".对应的MainActivity"
android:label="This is FirstActivity"
android:launchMode="singleTask" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
注意:如果onCreate()方法中如果需要各种访问权限,那么久必须添加
<uses-permission android:name="..."/>
app就是从这里开始完成启动的,没有经过注册的活动,都会以报错或崩溃结束虚拟机
活动的生命周期:
MainActivity:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
public class MainActivity extends Activity {
public static final String TGA = "MainActivity";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TGA, "onCreate");
Log.d(TGA, this.toString());
requestWindowFeature(Window.FEATURE_NO_TITLE);//设置标题栏不显示
setContentView(R.layout.activity_main);
if(savedInstanceState!=null){
String tempdata = savedInstanceState.getString("data_key");
Log.d(TGA, tempdata);
}
Button startNormalActivity = (Button) findViewById(R.id.start_normal_activity);
Button startDialogActivity = (Button) findViewById(R.id.start_dialog_activity);
startNormalActivity.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,
NormalActivity.class);
//Intent 作为一个传输工具,可以传输到另一个Activity Service Broadcast 等等
//对应的下面startActivty(intent); startService(intent);.......
startActivity(intent);
}
});
startDialogActivity.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,
DialogActivity.class);
startActivity(intent);
}
});
}
//保证在上一次的数据不会因为Onstop()的时候被系统回收掉
//所以调用onSaveInstanceState()这个方法保存一下
protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
String tempdata="someting you have typed";
outState.putString("data_key", tempdata);
}
protected void onStart(){
super.onStart();
Log.d(TGA, "onStart");
}
protected void onResume(){
super.onResume();
Log.d(TGA, "onResume");
}
protected void onPause(){
super.onPause();
Log.d(TGA, "onPause");
}
protected void onStop(){
super.onStop();
Log.d(TGA, "onStop");
}
protected void onDestroy(){
super.onDestroy();
Log.d(TGA, "onDestroy");
}
protected void onRestart(){
super.onRestart();
Log.d(TGA, "onRestart");
}
}
//DialogActivity:
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class DialogActivity extends Activity {
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dialog_activity);
}
}
//NormalActivity:
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class NormalActivity extends Activity {
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.normal_activity);
}
}
res下的layout:
dialog_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is a dialog activity"/>
</LinearLayout>
normal_activty.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is a normal activity"/>
</LinearLayout>
AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.baozhong.activitycycle"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- 用android:theme属性给注册两个活动 -->
<activity
android:name="com.baozhong.activitycycle.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 显式-->
<activity android:name=".NormalActivity">
</activity>
<activity android:name=".DialogActivity"
android:theme="@android:style/Theme.Dialog">
</activity>
</application>
</manifest>
结果如下:
开启MainActivtiy
05-16 20:42:49.287: D/MainActivity(8146): onCreate
05-16 20:42:49.309: D/MainActivity(8146): onStart
05-16 20:42:49.309: D/MainActivity(8146): onResume
另一个活动到达前台的时候
05-16 20:45:14.200: D/MainActivity(8146): onPause
05-16 20:45:14.679: D/MainActivity(8146): onStop
此时MainActivity活动不可见
当从第二活动返回的时候,MainActivty再次回到前台,再次显示
05-16 20:47:03.389: D/MainActivity(8146): onRestart
05-16 20:47:03.389: D/MainActivity(8146): onStart
05-16 20:47:03.389: D/MainActivity(8146): onResume
当按下MainActivtiy活动的返回键时,活动销毁:
05-16 20:48:41.859: D/MainActivity(8146): onPause
05-16 20:48:42.295: D/MainActivity(8146): onStop
05-16 20:48:42.298: D/MainActivity(8146): onDestroy
活动周期图:
活动的启动模式:android:launchMode=”singleTop”
standard:打开多少个活动就按对应多少次的返回键,严格按照栈的顺序
singleTop:如果发现返回栈的栈顶已经是该活动,则不会新建再新建新的活动
singleTask:每次返回栈检查发现是否存在该活动实例,如果发现直接使用
并把这个活动之上的活动统统出栈,如果没有就新建一个新的活动实例
singleInstance:创建栈管理器,如有First—Second—Third
三个,将Second设置为singleInstance 也就相当于把First 和 Third 放在同一个栈A里
按Third返回键会返回First,再按First返回键会回到Second,此时栈A为空,再按Second的返回键将退出程序
Activity管理:
新建一个ActivityCollector:
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
public class ActivityCollector {
public static List<Activity> activities = new ArrayList<Activity>();
public static void addActivity(Activity activity) {
activities.add(activity);
}
public static void removeActivity(Activity activity) {
activities.remove(activity);
}
public static void finishAll() {
for (Activity activity : activities) {
if (!activity.isFinishing()) {
activity.finish();
}
}
}
}
再新建BaseActivity
public class BaseActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("BaseActivity", getClass().getSimpleName());
ActivityCollector.addActivity(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
ActivityCollector.removeActivity(this);
}
}
然后再将其他的Activity都继承BaseActivity ,然后再每个Activity中调用
ActivityCollector的方法就行,可以实现随时随地关闭活动等操作
Intent的显式和隐式:
<activity
android:name=".SecondActivity"
android:launchMode="singleInstance" >
<intent-filter>
<action android:name="com.example.activitytest.ACTION_START" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.example.activitytest.MY_CATEGORY" />
</intent-filter>
</activity>
/*
<category android:name="android.intent.category.DEFAULT" />必须的
只有一个action,可以有多个category
Intent intent = new Intent("com.example.activitytest.ACTION_START");*/
显式:
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
<activity android:name=".SecondActivity"></activity>