微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android:使用图标作为后退按钮,无需重新加载上一个活动

我有以下代码启用主页按钮作为后退按钮.我正在面对的问题是从这个活动,如果我使用真正的后退按钮,它只是回到以前的活动,就像我离开它.如果我使用主页按钮,它会重新加载页面,所以我失去了以前做过的事情.我确定这是一件简单的事,我失踪了.
@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.census_management_search,menu);
    ActionBar actionBar = getActionBar();
    actionBar.setdisplayHomeAsUpEnabled(true);
    return true;
}

@Override
public boolean onoptionsItemSelected(MenuItem item) 
{
    // Handle item selection
    switch (item.getItemId()) 
    {
        case android.R.id.home:
            Intent intent = new Intent(this,CensusManagementActivity.class);
            NavUtils.navigateUpTo(this,intent);
            return true;
        default:
            return super.onoptionsItemSelected(item);
    }
}

解决方法

而不是Intent和NavUtils尝试使用onBackpressed()方法.
@Override
public boolean onoptionsItemSelected(MenuItem item) 
{
    // Handle item selection
    switch (item.getItemId()) 
    {
        case android.R.id.home:
            onBackpressed();
            return true;
        default:
            return super.onoptionsItemSelected(item);
    }
}

原文地址:https://www.jb51.cc/android/312554.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐