我想为我的Android应用程序定义一个向上按钮以转到父活动。从某些资源中,我在点击向上按钮时找到了这段代码:
NavUtils.navigateUpFromSameTask(getActivity());
但我有一个问题,那就是我的活动需要一些数据用于创建(Intent extras),因此我用这段代码替换了:
getActivity().finish();
但有些感觉告诉我这不是一种正确的方式来达到父活动,也许我将在以后遇到一些运行时错误。你觉得我该怎么办?
答案 0 :(得分:1)
Intent myIntent = new Intent(sourceActivity.this,targetActivity.class);
myIntent.putExtra("NameKey", yourValue); // for send a Value with a key name. yourValue can be every type value
startActivity(myIntent);
this.finish();
// and for receive value from target activity
Bundle extraGet = getIntent().getExtras();
String receiveValue = extraGet.getString("NameKey");
你也可以定义public static
你的变量,它可以将它用于每个类和活动
我想你想要这个:
Intent intent = NavUtils.getParentActivityIntent(this);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
NavUtils.navigateUpTo(this, intent);
答案 1 :(得分:0)
//this function can used for every key on Phone that pressed
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==event.KEYCODE_VOLUME_UP){
// On Click KEYCODE_VOLUME_UP Commands
// here, every code want to execute
}
return super.onKeyDown(keyCode, event);
}
更新(您想要的;-)) in onCreate:
getActionBar().setDisplayHomeAsUpEnabled(true);
主要课程中的:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Toast.makeText(getApplicationContext(), "Khosh begzare", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}