通知访问后允许返回导航

时间:2016-04-01 18:01:03

标签: android notifications onbackpressed

我的应用中有通知,点击后,允许用户进入消息传递活动。目前,在按下后退导航时,它只会关闭应用程序和活动。

如何配置它,以便在按下后退按钮后,它会打开主要活动(有抽屉导航)以及打开特定的好友消息列表片段? (并随后允许在应用程序内进一步导航)

1 个答案:

答案 0 :(得分:0)

我在应用中处理此问题的方法是覆盖onKeyDown()方法。你也可以覆盖onBackPressed(),但这在过去给我带来了问题,所以我这样做。

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode == KeyEvent.KEYCODE_BACK) {
        //do any cleanup you need to do
        Intent upIntent = new Intent(this, MainActivity.class);
        startActivity(upIntent);
        return true;
    } else {
        return false;
    }
}

要处理可以从多个其他活动启动的活动中的导航,我通常会传入一个表示"签名的字符串"启动当前活动的Activity,存储它,然后稍后检查以找出向上导航按钮应导航到的位置。然后在我的onOptionsItemSelected()方法中,我使用存储的"呼叫签名"检查哪个活动启动了此活动。字符串,然后回到它。像这样......

@Override
 public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    switch(id) {
        case android.R.id.home:
            Intent upIntent = null;
            switch(callingActivitySignature) {
                case MainActivity.CALL_SIGNATURE:
                    upIntent = new Intent(this, MainActivity.class);
                    break;
                case SecondActivity.CALL_SIGNATURE:
                    upIntent = new Intent(this, SecondActivity.class);
                    break;
                case ThirdActivity.CALL_SIGNATURE:
                    upIntent = new Intent(this, ThirdActivity.class);
                    break;
            }

            if(upIntent != null) {
                startActivity(upIntent);
            }
            return true;
    }
    return false;
}