前言:生命中有许多人陪伴自己走过一生中的某段旅程,只是有些人只是某阶段出现,有些人却陪伴自己很久。就像小学、中学、高中、大学,那些曾经以为会长久拥有的,当经历过天涯各地地忙碌于生活,或如意,或失意;渐渐地那些画面只剩回忆。天涯各自安命,能在一起的就尽力珍惜吧,不在一起地就衷心地祝福,我们都需要一种姿态生活下去!Android中的Activity的生命中,我们经常需要打开另外一个Activity,即另外一个界面。这个可能出现的时间很短暂,然后又回到主界面。但是这两个Activity都在各自的生命周期中经历了生命中的某个阶段。
该项目在上篇项目中添加相关内容,进行演示,项目结构如下:
第一步:在layout中添加另外一个界面布局,即activity_sub.xml,代码内容如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/close"
android:layout_below="@id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/close"
/>
</RelativeLayout>
当然主界面activity_sub.xml内容重新布局下,代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/open"
android:layout_below="@id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/open"
/>
</RelativeLayout>
第二步:在values目录下的strings.xml中编写代码内容如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">TestActivity</string>
<string name="hello_world">Test Activity life cycle</string>
<string name="close">关闭</string>
<string name="open">打开另外一个界面</string>
</resources>
注:这个文件中的东西是layout界面布局中要用到的东西;比如android:text="@string/open"中的open就是对应<string name="open">打开另外一个界面</string>,这个界面布局完后,程序界面按钮就会显示:打开另外一个界面
第三步:在src中新建一个文件SubActivity.java,代码编写如下:
package com.wyz.testactivity;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SubActivity extends Activity {
private static final String TAG="SubActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub);
Log.d(TAG, "生命周期--创建阶段(onCreate)");
Button btn_close = (Button)findViewById(R.id.close);
btn_close.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
SubActivity.this.finish(); //关闭
}
});
}
protected void onStart()
{
super.onStart();
Log.d(TAG, "生命周期--开始阶段(onStart)");
}
protected void onResume()
{
super.onResume();
Log.d(TAG, "生命周期--恢复阶段(onResume)");
}
protected void onPause()
{
super.onPause();
Log.d(TAG, "生命周期--暂停创建阶段(onPause)");
}
protected void onStop()
{
super.onStop();
Log.d(TAG, "生命周期--停止创建阶段(onStop)");
}
protected void onRestart()
{
super.onRestart();
Log.d(TAG, "生命周期--重启阶段(onRestart)");
}
protected void onDestory()
{
super.onDestroy();
Log.d(TAG, "生命周期--销毁阶段(onDestory)");
}
}
接着我们也要修改下MainActivity.java,让它可以打开子界面:
package com.wyz.testactivity;
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.widget.Button;
public class MainActivity extends Activity {
private static final String TAG="MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "生命周期--创建阶段(onCreate)");
Button btn_open = (Button)findViewById(R.id.open);
btn_open.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, SubActivity.class)); //打开另外一个界面
}
});
}
protected void onStart()
{
super.onStart();
Log.d(TAG, "生命周期--开始阶段(onStart)");
}
protected void onResume()
{
super.onResume();
Log.d(TAG, "生命周期--恢复阶段(onResume)");
}
protected void onPause()
{
super.onPause();
Log.d(TAG, "生命周期--暂停创建阶段(onPause)");
}
protected void onStop()
{
super.onStop();
Log.d(TAG, "生命周期--停止创建阶段(onStop)");
}
protected void onRestart()
{
super.onRestart();
Log.d(TAG, "生命周期--重启阶段(onRestart)");
}
protected void onDestory()
{
super.onDestroy();
Log.d(TAG, "生命周期--销毁阶段(onDestory)");
}
}
最后一步:修改下AndroidManifest.xml,在</application>节点前添加内容如下:
<activity
android:name=".SubActivity"
android:label="@string/app_name" >
</activity>
这个添加的作用是:让程序知道有另外一个界面存在了!
注:android:name就是子界面的类名
界面效果如下: