对于另一个活动类的意图的活动,不调用onOptionsItemSelected

时间:2014-01-20 13:45:09

标签: java android android-intent view android-activity

我想在我的测试应用程序onOptionsItemSeleceted method()中使用库活动,但我发现如果我使用intent。 Android使用库活动的onOptionsItemSeleceted方法()不是我的测试活动方法()。我做了一个测试应用程序,我的MainActivity是这样的:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent intent = new Intent (this, Test_1.class);
    startActivity(intent);



}


@Override
public boolean onCreateOptionsMenu(Menu menu) {


    getMenuInflater().inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case R.id.action_setting:
            Toast.makeText(getApplicationContext(),
                    "Button is clicked", Toast.LENGTH_LONG).show();

            return true;
        case R.id.action_setting1:
            Toast.makeText(getApplicationContext(),
                    "Button is clicked", Toast.LENGTH_LONG).show();
            return true;
        case R.id.action_settings:
            Toast.makeText(getApplicationContext(),
                    "Button is clicked", Toast.LENGTH_LONG).show();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

我的测试活动类就像:

public class Test_1 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test_1);

    TextView txt = (TextView)findViewById(R.id.textView);
    txt.setText("This is new page");

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.test_1, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return false;
    }
    return super.onOptionsItemSelected(item);
}

我想在Main Activity类中使用onOptionsItemSelected()方法,但我无法在屏幕上看到我的Toast消息。 如果你能帮助我,我真的很感激。谢谢:))

2 个答案:

答案 0 :(得分:0)

嗯,也许它会通过?

default:
      return super.onOptionsItemSelected(item);

我不确定R.menu.main中有什么 但要确保有R.id.action_setting,R.id.action_setting1,R.id.action_settings。

答案 1 :(得分:0)

SUPER:

它在子类方法定义中用于调用超类中定义的方法。 在您的情况下,Activity是一个超级类Test_1 onOptionsItemSelected的活动将被调用..而不是MainActivity onOptionsItemSelected

只需添加此内容即可查看您的Toast消息。在Test_1 onOptionsItemSelected类中:

switch (item.getItemId()) {
        case R.id.action_setting:
            Toast.makeText(getApplicationContext(),
                    "Button is clicked", Toast.LENGTH_LONG).show();

        return true;
    case R.id.action_setting1:
        Toast.makeText(getApplicationContext(),
                "Button is clicked", Toast.LENGTH_LONG).show();
        return true;
    case R.id.action_settings:
        Toast.makeText(getApplicationContext(),
                "Button is clicked", Toast.LENGTH_LONG).show();
        return true;
    default:
        return super.onOptionsItemSelected(item);
}