仿QQ退出程序,不退出主页面

无意中看见的。。。。https://blog.csdn.net/u010350891/article/details/83415761

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0)
    {
        finish();
        return;
    }
    setContentView(R.layout.activity_loading);
}

.

//点击返回键返回桌面而不是退出程序
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    if (keyCode == KeyEvent.KEYCODE_BACK) {
        Intent home = new Intent(Intent.ACTION_MAIN);
        home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        home.addCategory(Intent.CATEGORY_HOME);
        startActivity(home);
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

推荐阅读更多精彩内容