退出应用的第N+1种方法-一行代码退出应用

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/gongziwushuang/article/details/51970015

退出应用的第N+1种方法

前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的布局文件如下

<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"
    tools:context="com.zhuyux.csdntest.MainActivity">

   <Button
       android:id="@+id/open"
       android:textSize="25dp"
       android:text="k开启"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />
</RelativeLayout>

最后一个的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的布局文件

<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"
    tools:context="com.zhuyux.csdntest.FourActivity">
    <Button
        android:id="@+id/close"
        android:textSize="25dp"
        android:text="关闭"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

别忘记载清单文件中注册,运行依次点击按钮,就会跳到最后一个界面
如图
最后一个界面
点击关闭按钮,则退出应用。感觉是不是很简单\

异常现象

回到官方的介绍最后一句

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);

点击关闭,就会报一下异常
点击关闭后报的异常
当然这丝毫不影响我们使用,因为我们的目的是推出应用,而不是返回结果给项目中的某个界面

猜你喜欢

转载自blog.csdn.net/gongziwushuang/article/details/51970015
今日推荐