MainActivity如下:
package cn.testfinish; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; /** * Demo描述: * 覆写Activity的finish()方法 * 一般情况下当调用finish()方法时,会调用onDestroy() * 当覆写了Activity的finish()方法后,在执行finish() * 方法时会先调用覆写的finish()再调用onDestroy()方法. */ public class MainActivity extends Activity { private Button mButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ mButton=(Button) findViewById(R.id.button); mButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { MainActivity.this.finish(); } }); } @Override public void finish() { super.finish(); System.out.println("调用了覆写的finish()方法"); } @Override protected void onDestroy() { super.onDestroy(); System.out.println("调用了Activity的onDestroy()方法"); } }
main.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" > <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="click here" android:layout_centerInParent="true" /> </RelativeLayout>
时间: 2024-10-07 20:36:55