Android:使用图标作为后退按钮而不重新加载以前的活动

时间:2013-04-18 21:25:19

标签: android android-intent android-activity

我有以下代码启用主页按钮作为后退按钮。我面临的问题是来自这个活动,如果我使用真正的后退按钮它就像我离开它时简单地回到之前的活动。如果我使用主页按钮,它正在重新加载页面,所以我失去了以前对它做的事情。我确信这很简单,我很遗憾。

@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);
    }
}

2 个答案:

答案 0 :(得分:15)

而不是IntentNavUtils尝试使用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);
    }
}

答案 1 :(得分:3)

Home / Up按钮应该重新加载新活动。但是,如果要模拟后退按钮功能,可以调用finish()返回上一个活动:

 case android.R.id.home:
        finish();
        return true;