How to put application to background?

允我心安 提交于 2019-12-13 18:35:06

问题


How to put application (activity?) to background so it can work there?

moveTaskToBack(true); doesn't work. I have android:noHistory="true" and this code:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) 
{
    switch(keyCode)
    {
        case KeyEvent.KEYCODE_BACK:
            moveTaskToBack(true);
            return true;
    }
    return false;
}

What is wrong? How to put application to background?

upd: or maybe I can use moveTaskToBack(true) only in root (MAIN, LAUNCHER) activity?


回答1:


If you want to jump to the home screen because of some activity you can do this:

    Intent i = new Intent(Intent.ACTION_MAIN);
    i.addCategory(Intent.CATEGORY_HOME);
    startActivity(i);

Just make sure you are running the code from your main activity thread. The "startActivity" implies this function is running from the main activity.




回答2:


activity are not made for background processes. Use Service for your background processes.



来源:https://stackoverflow.com/questions/11495188/how-to-put-application-to-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!