当用户在一个Activity中执行完任务,要向前切换到另一个Activity,并且不希望用户点击“返回”按钮再回到当前的Activity时,可以调用Activity类的finish()方法,把当前的Activity从该应用的回退堆栈中删除,finish()方法被调用,当前的Activity就会被销毁,用户不能够再按“返回”按钮回到这个Activity中。示例代码如下:
一.清单文件(AndroidManifest.xml)
<?xmlversion="1.0"encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="my.android.test"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon"android:label="@string/app_name">
<activity android:name=".Forwarding"
android:label="@string/app_name">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>
<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".ForwardTarget">
</activity>
</application>
<uses-sdk android:minSdkVersion="9"/>
</manifest>
二.字符资源定义(strings.xml)
<?xmlversion="1.0"encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Forwarding!</string>
<string name="app_name">Forwarding</string>
<string name="activity_forwarding">App/Activity/Forwarding</string>
<string name="forwarding">Press the button to go forward to the next activity. This activity will stop, so you will no longer see it when going back.</string>
<string name="go">Go</string>
<string name="forward_target">Press back button and notice we don\'t see the previous activity.</string>
</resources>
三.布局定义(forwarding.xml、forward_target.xml)
Forwarding.xml
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:padding="4dip"
android:gravity="center_horizontal"
android:layout_width="match_parent"android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"android:layout_height="wrap_content"
android:layout_weight="0"
android:paddingBottom="4dip"
android:text="@string/forwarding"/>
<Buttonandroid:id="@+id/go"
android:layout_width="wrap_content"android:layout_height="wrap_content"
android:text="@string/go">
<requestFocus />
</Button>
</LinearLayout>
forward_target.xml
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:padding="4dip"
android:gravity="center_horizontal"
android:layout_width="match_parent"android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"android:layout_height="wrap_content"
android:layout_weight="0"
android:text="@string/forward_target"/>
</LinearLayout>
四.Activity类定义(Forwarding.java、ForwardTarget.java)
Frowarding.java
package my.android.test;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Forwarding extends Activity {
/** Activity被首次创建时调用这个方法*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.forwarding);
//从布局中查找go按钮,并给它设置点击事件监听器
Button goButton = (Button)findViewById(R.id.go);
goButton.setOnClickListener(mGoListener);
}
/** 实现OnClickListener接口,监听按钮的点击事件*/
private OnClickListener mGoListener = new OnClickListener(){
public void onClick(View v){
//声明Intent对象,并启动ForwardTarget Activity
Intent intent = new Intent();
intent.setClass(Forwarding.this, ForwardTarget.class);
startActivity(intent);
//从历史堆栈中删除当前Activity,用户点击“返回”按钮键时,不会再返回到这个Activity。
finish();
}
};
}
ForwardTarget.java
package my.android.test;
import android.app.Activity;
import android.os.Bundle;
publicclass ForwardTargetextends Activity {
@Override
protectedvoid onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
setContentView(R.layout.forward_target);
}
}