杀死后退按钮活动按钮

2023-09-12 22:12:00 作者:酒吞童子

好吧,我得到了一个活动,它启动时,它加载图像从互联网上,并试图以节省内存,当活动是左(后退按钮的存在pressed),我想活动转储(摆脱所有的字符串,并且在它的图像)。所以,我想做到这一点的最好办法就是杀了活动。好吧,我似乎无法找出回调时,后退按钮是pressed,所以我一直在试图使用的onPause和的onStop回调的任务,但糟蹋的方式强行关闭我的应用程序。我试着这样

Ok, I got an activity, that when it starts, it loads an image from the internet, and, trying to save memory, when the activity is left (the back button being pressed), I want the activity to dump (get rid of all the strings and images that are in it). So, I figure the best way to do this is to just kill the activity. Well, I cant seem to figure out the callback for when the backbutton is pressed, so I have been trying to use the onPause and onStop callbacks for the task, but botch ways force close my app. Im trying this

public void onPause() {
this.finish();
}
public void onStop() {
finish();
}

香港专业教育学院尝试了这种多变化,但他们都不工作。有任何想法吗?

Ive tried multiple variations of this, but none of them seem to work. Any ideas?

推荐答案

添加到您的活动

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if ((keyCode == KeyEvent.KEYCODE_BACK))
    {
        finish();
    }
    return super.onKeyDown(keyCode, event);
}