【有书共读】《Android 7 编程入门经典(第四版) 》
《Android 7 编程入门经典(第四版) 使用Android studio 2 》
第三章
一个Android应用可以不适用Activity或者包含多个Activity.通常来说,应用包含一个或多个Activity.Activity的主要目的是为用户提供交互.从一个Activity显示到屏幕上到他消失,这个过程经历了多个阶段,这个阶段被称为Actiity的生命周期.为了确保应用可以正常工作,了解Activity的生命周期是非常重要的,除了Activity,Androud 4.0开始在手机上支持这个特性.可以把Fragment理解为微型的Activity,可将多个Fragment组成一个Activity..
除了Activity,Android另外一个比较独特的概念是Intent.Intent就如同双面胶一样把不同应用的Activity粘在一起,使他们可以一起无缝的工作,并且确保任务都可以执行,就像他们都属于一个应用.
Activity
创建Activity首先要新创建一个继承Activity基类的Java类.
Activity类从res/layout文件夹中加载由XML文件定义的用户界面(UI)组件.
应用中的每一个Acticity必须在AndroidManifest.xml文件中声明
Activity基类定义了一系列事件,管理着Activity的生命周期
Activity类定义了以下事件
1. onCreate()——当首次创建Activity时被调用
2. onStart()——当Activity显示在用户面前时被调用
3. onResume()——当Activity能够开始于用户交互时被调用
4. onPause()——在当前的Activity将暂停并且前一个Activity将要被重新开始时被调用
5. onStop()——在Activity不再显示在用户面前时被调用
6. onDestroy()——当Activity被系统销毁时调用(手动销毁或者系统回收内存时)
7. onRestart()——当Activity停止后并重新启动时被调用
在默认情况下,新建的Activity都包含onCreate()事件,这个时间处理程序中的代码将有助于在屏幕上显示UI元素
package com.example.zhengwei.threeone;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
String tag="Lifecycle Step"; @Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout._activity_main_);
Log._d_(tag,"In the onCreate() event");
} @Override public void onStart(){
super.onStart();
Log._d_(tag,"In the onStart() event");
}
public void onRestart(){
super.onRestart();
Log._d_(tag,"In the onRestart() event");
}
public void onResume(){
super.onResume();
Log._d_(tag,"In the onResume() event");
}
public void onPause(){
super.onPause();
Log._d_(tag,"In the onPause() event");
}
public void onStop(){
super.onStop();
Log._d_(tag,"In the onStop() event");
}
public void onDestroy(){
super.onDestroy();
Log._d_(tag,"In the onDestroy() event");
}
//首次载入时:调用 onCreate().onStart()
//点击菜单键:调用 onPause.onStop()
//再次点击菜单键:调用 onStart(),onStart(),onResume()
//点击返回键:调用 onPause(),onStop()onDestroy()
//点击Home键:调用 onPause(),onStop() /*竟然没有被销毁,可怕!!!*/
} 经过安卓虚拟机测试:
1.单击Back按钮时,Activity会被销毁 因为无论Activity现在处于什么状态都会丢失,也就是说你需要在Activity中写一些代码,当它被销毁时保存它的状态.
2. onPause()方法在下面两种状态会被调用:
a) 当Activity被发送到后台时
b) 当用户单击back按钮关闭Activity时
3. 当一个Activity启动时,onStart()和onResume()方法总被调用,无论这个Activity是从后台被恢复的还是新建的.
4. 当一个Activity首次创建时,会调用onCreate()方法
5. 使用onCreate()方法新建和实例化在应用中使用的对象
6. 使用onResume()方法启动那些当Activity在前台时需要运行的服务和代码
7. 使用onPause()方法停止那些当Acticity不在前台时不需要运行的服务和代码
8. 使用onDestory()方法在Activity销毁前释放资源
注意:即使一个应用只有一个Activity并且这个Activity已经被杀死,这个应用仍然在内存中运行.
Activity使用默认的Android主题,然而,近几年一直在推广使用一个称为Material的主题.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.zhengwei.activity101"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@android:style/Theme.NoTitleBar"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
package com.example.zhengwei.activity101; import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
String tag="Lifecycle Step";
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(tag,"In the onCreate() event");
}
}
package com.example.zhengwei.activity101;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
} requestWindowFeeature(Window.FEATURE_NO_TITLE); <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.zhengwei.activity101"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@android:style/Theme.NoTitleBar"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.zhengwei.Dialog" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme" > <activity android:name=".DialogActivity" android:label="@string/app_name" android:theme="@style/Theme.AppCompat.Dialog" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
package com.example.zhengwei.Dialog import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
public class DialogActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_dialog, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
} <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="package="com.jfdimarzio.activity101> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>修改Activity101.java
package com.jfdimarzio.activity101 import android.app.Activity;
import android.app.ProgressDialog;
import android.os.CountDownTimer;
import android.os.Bundle;
public class MainActivity extends Activity {
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onStart()
{
super.onStart();
progressDialog = ProgressDialog.show(this,"Please Wait", "Processing...",true);
CountDownTimer timer = new CountDownTimer(3000,1000) {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
progressDialog.dismiss();
}
}.start();
}
}
查看7道真题和解析