我在一个活动中有一个后退按钮,当我点击它返回到父活动时,它将重置父活动。就像onCreate()
被再次调用一样。我不确定为什么会这样,因为当您点击“后退”按钮时,它只是调用finish()
存在的当前活动。
这是我声明工具栏的方式:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_item);
Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
if(getSupportActionBar() != null)
{
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
当您点击按钮时,将发生以下情况:
public boolean onOptionsItemSelected(MenuItem item)
{
if (item.getItemId()==android.R.id.home)
{
finish();
}
return super.onOptionsItemSelected(item);
}
奇怪的是,当我按下“保存”按钮时,我没有任何重置就返回到父活动。所以我不确定为什么会这样。
您需要返回true,否则它将始终调用该onCreat()
方法。同样,您可以创建一个“空”意图,而不必在返回true的活动上对其进行处理。
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if (item.getItemId()==android.R.id.home)
{
Intent intent = new Intent();
setResult(Intent_Constant.TAPPED_BACK_BUTTON, intent);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句