我有以下代码启用主页按钮作为后退按钮.我正在面对的问题是从这个活动,如果我使用真正的后退按钮,它只是回到以前的活动,就像我离开它.如果我使用主页按钮,它会重新加载页面,所以我失去了以前做过的事情.我确定这是一件简单的事,我失踪了.
@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);
}
}