的onPause()的Andr​​oid [英] onPause() Android

查看:130
本文介绍了的onPause()的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个连接到一个URL每X秒在onPostExecute,我做了一个图书馆的应用。当我关闭应用程序或返回的返回按钮,它必须与主活动类的的onPause()方法的重写停止。
我想用我自己的图书馆,以方便类新的开发人员创建,以控制这一点,但如果我重写它的库的onPause()方法,它将继续进行连接。
有一个办法做到这在我的图书馆吗?

下面的主类我的code:

  @覆盖
公共无效的onPause(){
    super.onPause();
    myLib.sto $ P $的PSource();
    myLib.flagRefresh = FALSE;
}@覆盖
公共布尔的onkeydown(INT键code,KeyEvent的事件){
     如果(键code == KeyEvent.KEY code_BACK){
        myLib.sto $ P $的PSource();
        myLib.flagRefresh = FALSE;
        super.onBack pressed();
        返回true;
     }
     返回super.onKeyDown(键code,事件);
}


解决方案

您应该使用的onStop()而不是的onPause(),只要在屏幕上没有显示该活动被称为,但仍在运行,即当屏幕变黑。而不是覆盖的的onkeydown()这是更好地使用 onBack pressed()的方法,但是如果你的地方您当前的code 的onPause()的onStop()它不会被要求将其覆盖。

I have an app that connects to an URL every X seconds in an onPostExecute of a library that I had done. When I close the app or go back with the back button, it must stop with an override of the onPause() method on the main activity class. I want to control this with my own library to facilitate the creation of class for new developers, but if I override it on the library onPause() method, it continues making connections. There's a way to do these on my library?

Here my code on the main class:

@Override
public void onPause() {
    super.onPause();
    myLib.stopResource();
    myLib.flagRefresh = false;
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
     if (keyCode == KeyEvent.KEYCODE_BACK) {
        myLib.stopResource();
        myLib.flagRefresh = false;
        super.onBackPressed();
        return true;
     }
     return super.onKeyDown(keyCode, event);    
}

解决方案

You should use onStop() instead of onPause(), which is called whenever the activity is not shown on screen but is still running, i.e. when the screen goes black. Instead of overwriting onKeyDown() it is better to use onBackPressed() method, however if you place your current code of onPause() inside onStop() it would not be required to overwrite it.

这篇关于的onPause()的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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