程序运行时我直接就从MainActivity跳转到BActivity,在BActivity有个“退出程序”菜单,点击会退出程序。具体过程如下:
 
1.jpg
 
 
 
第二步:定义一个工具类Sysutil用来退出程序,这样定义好处可以在多个Activity的时候复用,代码如下:
 
package com.gt.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

// 这里用来接受退出程序的指令
@Override
protected void onStart() {
int flag = getIntent().getIntExtra("flag", 0);
System.out.println("onStart"+flag);
if (flag == SysUtil.EXIT_APPLICATION) {
finish();
}
super.onResume();
}

// 程序启动后就进入了BActivity
@Override
protected void onResume() {
Intent mIntent = new Intent();
mIntent.setClass(this, BActivity.class);
startActivity(mIntent);
super.onStart();
}

// 当activity是单例的时候,再次启动该activity就不会再调用 oncreate->onstart这些方法了
@Override
protected void onNewIntent(Intent intent) {
int flag = intent.getIntExtra("flag", 0);
System.out.println("onNewIntent"+flag);
if (flag == SysUtil.EXIT_APPLICATION) {
System.out.println("关闭");
finish();
}
super.onNewIntent(intent);
}
}
 
 
package com.gt.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class BActivity extends Activity {
  public static final int MENU_EXITAPPLICATION = Menu.FIRST;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    TextView mTextView = new TextView(this);
    mTextView.setText("Welcome to Frankie's blog.");
    setContentView(mTextView);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(0, MENU_EXITAPPLICATION, 0, "退出程序");
    return super.onCreateOptionsMenu(menu);
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == MENU_EXITAPPLICATION) {
      // 其他Activity退出程序这样调用就ok了
      SysUtil mSysUtil = new SysUtil(BActivity.this);
      mSysUtil.exit();
      System.out.println("onOptionsItemSelected");
    }
    return super.onOptionsItemSelected(item);
  }
}
 
 
 
package com.gt.test;

import android.content.Context;
import android.content.Intent;

public class SysUtil {
  public static final int EXIT_APPLICATION = 0x0001;

  private Context mContext;

  public SysUtil(Context context) {
    this.mContext = context;
  }

  // 完全退出应用
  public void exit() {
    Intent mIntent = new Intent();
    mIntent.setClass(mContext, MainActivity.class);
    // 这里设置flag还是比较 重要的
    mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    // 发出退出程序指示
    mIntent.putExtra("flag", EXIT_APPLICATION);
    mContext.startActivity(mIntent);
    System.out.println("exit");    
  }
}
 
 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.gt.test" android:versionCode="1" android:versionName="1.0">
  <uses-sdk android:minSdkVersion="9" />

  <application android:icon="@drawable/icon" android:label="@string/app_name">

    <activity android:name=".MainActivity" android:launchMode="singleTop"
      android:label="@string/app_name">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    
    <activity android:name=".BActivity"/>
  </application>
</manifest>