The first n methods of exiting the application of the N+1 method
Prior to the online understanding of the exit application methods, including the registration of the closed interface in each activity of the broadcast recipient, when you want to launch an application to send a broadcast off all the interface, the most common use list to simulate the task stack to manage activity, To exit the app, use traverse to close all activity. Of course, there are some divergent thinking to end with throwing anomalies. Others say use Startactivityforresult to turn on each activity and then onactivityresult inside to close each interface. Methods are various, we can search on the Internet, where the author will not repeat the following, there are readers to introduce the first n+1 method
Finishaffinity () Introduction
The core of the N+1 method is the Finishaffinity () method, and the following is an official explanation:
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.
Good English can be directly read the above introduction, if the English is not good, the author of the university's four level has not been the English level for everyone to turn, if not, please criticize correct. Here is the translation
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, 如果用户已经跳往另外一个应用的activitysall 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.如果你这样做的话会抛出异常
The above is the ' finishaffinity () ' Introduction, below we directly on the code.
Exiting an application instance
First build a baseactivity and then build four activities that inherit from baseactivity, such as:
The baseactivity code is very simple and the code is as follows:
package com.zhuyux.csdntest;import android.app.Activity;/** * Created by xiaozhu on 2016/7/20. */public class BaseActivity extends Activity {}
The code for the child activity is as follows
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;@Overrideprotected 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);}@Overridepublic void onClick(View v) { switch (v.getId()) { case R.id.open: startActivity(new Intent(MainActivity.this,SecondActivity.class)); break; }}}
The layout file for the child activity is as follows
<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>
The code for the last activity is as follows
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;@Overrideprotected 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")@Overridepublic void onClick(View v) { switch (v.getId()) { case R.id.close: //退出应用核心类 finishAffinity(); break; }}}
The last activity layout file
<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>
Do not forget to register the manifest file, run the click button, will jump to the last screen
Click the Close button to exit the app. The feeling is not very simple \
Abnormal phenomena
Back to the official introduction last sentence
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.如果你这样做的话会抛出异常
Under what circumstances will it be abnormal? First, we modify the code inside the thirstactivity to put the code inside
startActivity(new Intent(ThirstActivity.this,FourActivity.class));
Replaced by
startActivityForResult(new Intent(ThirstActivity.this,FourActivity.class),0);
and add the following code
@Overrideprotected 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(); }}
Then fouractivity the finishaffinity () before adding the following code
Intent intent = getIntent(); intent.putExtra("data","我返回结果了"); setResult(2,intent);
Click Close, you will report the exception
This, of course, does not affect our use, as our aim is to launch the application instead of returning the results to an interface in the project
Exit Application N+1 Method-one line of code exits the app