如何摧毁Android的活动? [英] How to destroy an activity in Android?

查看:168
本文介绍了如何摧毁Android的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在应用程序运行时,我preSS HOME键来关闭应用程序。当我重新启动应用程序,将恢复之前,点击首页显示在页面上。我想在应用程序启动的初始显示代替。我已经使用面漆()来完成的活动,但它不工作。有什么建议?

While the application is running, I press the HOME button to close the application. When I start the application again, it resumes on the page displayed prior to clicking on HOME. I want the application to start with the initial display instead. I have used finish() to finish the activity but it is not working. Any suggestions?

推荐答案

您很有可能具有相同的活动的几个实例。要解决这样的问题,创建自己的父类的活动,例如MyRootActivity将容纳所有可用/活活动的静态列表:

Most likely you have several instances of the same activity. To resolve this kind of issues create your own parent Activity class e.g. MyRootActivity which will hold static list of all of available/alive activities:

public class MyRootActivity extends Activity
{
    private static final String TAG=MyRootActivity.class.getName();
    private static ArrayList<Activity> activities=new ArrayList<Activity>();


    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        activities.add(this);
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        activities.remove(this);
    }

    public static void finishAll()
    {
        for(Activity activity:activities)
           activity.finish();
    }
}

有关的所有活动必须MyRootActivity的儿童。

For that all of your activities need to be children of MyRootActivity.

然后,当你要确保你关闭你的应用程序 - 只需要调用MyRootActivity.finishAll();

Then when you are about to sure that you're closing your application - just call MyRootActivity.finishAll();

这篇关于如何摧毁Android的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆