Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 退出應用的第N+1種方法-一行代碼退出應用

退出應用的第N+1種方法-一行代碼退出應用

編輯:關於Android編程

前N種方法

之前有在網上了解過退出應用的方法,其中包括在每個activity中注冊關閉界面的廣播接受者,當想推出應用時發一條廣播關閉所有的界面,最常用的使用list去模擬任務棧的來管理activity,要退出應用使用遍歷關閉所有activity。當然也有人發散思維用拋異常來結束。還有人就說用 startActivityForResult開啟每個activity,然後再onActivityResult裡面關閉每個界面。方法多種多樣,大家都可以在網上搜,在這裡作者就不再贅述,下面就有來讀者介紹第N+1種方法

finishAffinity()介紹

第N+1方法的核心就是finishAffinity()方法,下面是官方的解釋:

    Finish this activity as well as all activities immediately below it in the current task that have the same affinity. This is typically used when an application can be launched on to another task (such as from an ACTION_VIEW of a content type it understands) and the user has used the up navigation to switch out of the current task and in to its own task. In this case, if the user has navigated down into any other activities of the second application, all of those should be removed from the original task as part of the task switch.
    Note that this finish does not allow you to deliver results to the previous activity, and an exception will be thrown if you are trying to do so.

英語好的可以可以直接看上面的英語介紹,如果英語不好,筆者就用大學裡四級沒過的英語水平給大家翻一下,如果不對,還請大家批評指正。下面是翻譯

Finish this activity as well as all activities immediately below it in the current task that have the same affinity.
關閉一個activity之後立即關閉當前任務棧中他下面那個具有相同血緣關系的activity的
This is typically used when an application can be launched on to another task
這個方法可以用來當一個應用想跳往另外一個棧時
(such as from an ACTION_VIEW of a content type it understands) 
這句話太難,就當樓主沒看見
and the user has used the up navigation to switch out of the current task and in to its own task. 
和用戶想跳出當前的棧並跳到自己的棧時
In this case, 
既然這樣
if the user has navigated down into any other activities of the second application, 
如果用戶已經跳往另外一個應用的activitys
all of those should be removed from the original task as part of the task switch.
所有的這個應用的activitys將會被移除
Note that this finish does not allow you to deliver results to the previous activity,
注意,這個方法不允許你返回結果給前一個activity,
 and an exception will be thrown if you are trying to do so.
如果你這樣做的話會拋出異常

以上就是‘ finishAffinity()’的介紹,下面我們直接上代碼。

退出應用實例

首先建立一個BaseActivity,然後建立四個繼承自BaseActivity的Activity,項目結構如下圖:
項目結構
BaseActivity代碼非常簡單,代碼如下:

package com.zhuyux.csdntest;

import android.app.Activity;

/**
 * Created by xiaozhu on 2016/7/20.
 */
public class BaseActivity extends Activity {
}

子Activity的代碼如下

package com.zhuyux.csdntest;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends BaseActivity implements View.OnClickListener {

private Button open;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
}

private void initView() {
    open = (Button) findViewById(R.id.open);

    open.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.open:
            startActivity(new Intent(MainActivity.this,SecondActivity.class));
            break;
    }
}

}

子Activity的布局文件如下

最後一個的Activity的代碼如下

package com.zhuyux.csdntest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class FourActivity extends AppCompatActivity implements View.OnClickListener {

private Button close;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_four);
    initView();
}

private void initView() {
    close = (Button) findViewById(R.id.close);

    close.setOnClickListener(this);
}

@SuppressLint("NewApi")
@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.close:
            //退出應用核心類
            finishAffinity();
            break;
    }
}
}

最後一個Activity的布局文件

別忘記載清單文件中注冊,運行依次點擊按鈕,就會跳到最後一個界面
如圖
最後一個界面
點擊關閉按鈕,則退出應用。感覺是不是很簡單\

異常現象

回到官方的介紹最後一句

Note that this finish does not allow you to deliver results to the previous activity,
注意,這個方法不允許你返回結果給前一個activity,
and an exception will be thrown if you are trying to do so.
如果你這樣做的話會拋出異常

什麼情況下會出異常呢?首先,我們修改一下ThirstActivity裡面的代碼,把裡面的代碼

startActivity(new Intent(ThirstActivity.this,FourActivity.class));

替換成

startActivityForResult(new Intent(ThirstActivity.this,FourActivity.class),0);

並添加以下代碼

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
    if (data!=null){
     Toast.makeText(ThirstActivity.this,     ""+data.getStringExtra("data"), Toast.LENGTH_SHORT).show();
        }
}

然後再FourActivity中 finishAffinity();之前添加如下代碼

 Intent intent = getIntent();
 intent.putExtra("data","我返回結果了");
 setResult(2,intent);

點擊關閉,就會報一下異常
點擊關閉後報的異常
當然這絲毫不影響我們使用,因為我們的目的是推出應用,而不是返回結果給項目中的某個界面

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved