android平台完全退出應用程式

來源:互聯網
上載者:User

  android退出應用程式會調用android.os.Process.killProcess(android.os.Process.myPid())或是System.exit(0),這隻是針對第一個Activity(也就是入口的Activity)時生效。如果有A,B,C三個Activity,而想在B或C中Activity退出,調用上面的方法,往往會銷毀當前的Activity返回上一個Activity。當然也可以逐個返回上一個Activity,直到跳轉到入口的Activity,最後退出應用程式。但這樣比較麻煩,而且逐個返回的體驗並不友好。

網上比較流行的方法是定義棧,寫一個ExitApplication類,利用單例模式管理Activity,在每個在Activity的onCreate()方法中調用ExitApplication.getInstance().addActivity(this)方法,在退出時調用ExitApplication.getInstance().exit()方法,就可以完全退出應用程式了。

import java.util.LinkedList;import java.util.List;import android.app.Activity;import android.app.Application;public class ExitApplication extends Application {private List activityList = new LinkedList();private static ExitApplication instance;private ExitApplication(){}//單例模式中擷取唯一的ExitApplication執行個體public static ExitApplication getInstance(){if(null == instance){instance = new ExitApplication();}return instance; }//添加Activity到容器中public void addActivity(Activity activity){activityList.add(activity);}//遍曆所有Activity並finishpublic void exit(){for(Activity activity:activityList){activity.finish();}System.exit(0);}}

下面的三個類IndexActivity, BActivity,CActivity是簡單的例子,分別是IndexActivity–>BActivity–>CActivity的跳轉順序。在每個Activity類中onCreate()方法中調用ExitApplication.getInstance().addActivity(Activity activity)方法。在任何一個Activity介面退出應用程式時,只要調用ExitApplication.getInstance().exit()方法,就可以在任何一個Activity中完全退出應用程式。

import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class IndexActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        Button next=(Button)findViewById(R.id.next_to_b);        next.setOnClickListener(nextClick);        Button exit=(Button)findViewById(R.id.exit_main);        exit.setOnClickListener(exitClick);        ExitApplication.getInstance().addActivity(this);    }    OnClickListener nextClick=new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubIntent intent=new Intent(IndexActivity.this,BActivity.class);startActivity(intent);}};    OnClickListener exitClick=new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubExitApplication.getInstance().exit();}};}

CActivity 類原始碼:

import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class CActivity extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.c);Button exit_c=(Button)findViewById(R.id.exit_c);exit_c.setOnClickListener(exitClick);ExitApplication.getInstance().addActivity(this);}OnClickListener exitClick=new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubExitApplication.getInstance().exit();//如果只是調用以下其中的一個方法,並不會完全退出應用//android.os.Process.killProcess(android.os.Process.myPid());//System.exit(0);}};}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.