0

After clicking a button to open a new Activity using this method:

setContentView(R.layout.activity_comunidades01);

The second Activity still display the same menu as the previous one.

I have already readed serval methods to fix it as the one related in here:

Android: How to enable/disable option menu item on button click?

But I discovered that the methods to intialize and create the menu are never called. I even try to follow this other link without success:

onCreateOptionsMenu is never called

I even deleted all the items in the menu.xml for this activity but still displaying the previous activity options.

I also clarify I am using android 4.4 as target API but level 10 as a minimum one since some devices that will be used are running android 2.3.

My second Activity is like this:

  public class SecondActivity extends Activity {

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

    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu){
        System.out.println("EN ON PREPARE OPTIONS MENU");
        (menu.findItem(R.id.sincronizar)).setEnabled(false);
        return super.onPrepareOptionsMenu(menu);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        System.out.println("EN ON CREATE OPTIONS MENU");
        getMenuInflater().inflate(R.menu.SecondActivity, menu);

        return true;
    }

}
Community
  • 1
  • 1
Michael Knight
  • 648
  • 2
  • 10
  • 26

2 Answers2

0

After clicking a button to open a new Activity using this method:

setContentView(R.layout.activity_comunidades01)

This is not how you open another activity! This way you only change the content of the current activity, nothing else.

Use Activity.startActivity() or Activity.startActivityForResult() as described in the docs to start another activity.

JimmyB
  • 12,101
  • 2
  • 28
  • 44
0

To start second activity use this:

Button bt = (Button)findViewById(R.id.bt);
bt.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View view) {
        Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
        startActivity(intent);
        //finish(); //if you want to close FirstActivity after showing SecondActivity
      }
 });