如何从历史回退堆栈中删除Activity自己

当用户在一个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.xmlforward_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.javaForwardTarget.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);

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值