public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu)
MenuItem item = menu.add(Menu.NONE, Menu.NONE, Menu.NONE, "Exit")
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener()
{
public boolean onMenuItemClick(MenuItem item)
{
System.exit(0)
return true
}
})
return true
}
2.这样太不方便,每次退出界面之后应用程做罩轿序还在运行,
网上提供了几种方法,将下面SysApplication这个类复制到工程里面,然后纯肆在每个Acitivity的oncreate方法里面通过SysApplication.getInstance().addActivity(this)添加当前Acitivity到ancivitylist里面去闷手,最后在想退出的时候调用SysApplication.getInstance().exit()可直接关闭所有的Acitivity并退出应用程序。
附代码:
import java.util.LinkedList
import java.util.List
import android.app.Activity
import android.app.AlertDialog
import android.app.Application
import android.content.DialogInterface
import android.content.Intent
public class SysApplication extends Application {
private List mList = new LinkedList()
private static SysApplication instance
private SysApplication() {
}
public synchronized static SysApplication getInstance() {
if (null == instance) {
instance = new SysApplication()
}
return instance
}
// add Activity
public void addActivity(Activity activity) {
mList.add(activity)
}
public void exit() {
try {
for (Activity activity : mList) {
if (activity != null)
activity.finish()
}
} catch (Exception e) {
e.printStackTrace()
} finally {
System.exit(0)
}
}
public void onLowMemory() {
super.onLowMemory()
System.gc()
}
}
3.在应用程序里面 的activity的oncreate里面添加SysApplication.getInstance().addActivity(this)
如:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.main)
SysApplication.getInstance().addActivity(this)
}
总体思路就是写在application中,每桥镇搭旅答个activity创建时添加进集合,点退出按钮,调用退出方法.不管怎么写,都离不开添加,遍历,销毁.....
package com.lindsay.application
import java.util.ArrayList
import java.util.List
import android.app.Activity
import android.app.Application
public class CloseActivityApplication extends Application
{
private List<Activity>activitys
// 每启动一个Activity---》放敏拿进集合
public void putActivity(Activity activity)
{
if (activitys == null)
{
activitys = new ArrayList<Activity>()
}
activitys.add(activity)
}
// 退出 *** 作
public void exit()
{
// 销毁Activity
try
{
for (Activity a : activitys)
{
if (a != null)
{
a.finish()
}
}
}
catch (Exception e)
{
e.printStackTrace()
}
finally
{
System.exit(0)
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)