0

我想为我的 android 应用程序定义一个向上按钮以转到父活动。单击向上按钮时,我从一些资源中找到了此代码:

NavUtils.navigateUpFromSameTask(getActivity());

但是我遇到了一个问题,我的活动需要一些数据来创建(Intent extras),因此我用以下代码替换:

getActivity().finish();

但是有些感觉告诉我,这不是访问父活动的正确方法,也许我以后会遇到一些运行时错误。你觉得我该怎么办?

4

2 回答 2

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); 
于 2015-10-10T18:07:31.710 回答
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);
          }

在 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);
    }
于 2015-10-10T17:21:54.620 回答