I have created an application with several activities. In all of the activities i overwrite to onOptionsItemSelected and onCreateOptionsMenu methods. As my onOptionsItemSelected method is quiet long i have to repeat that code segment in every activity. How can i create a single method that can be applied to all activites. Here is my onOptionsItemSelected and onCreateOptionsMenu methods.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
MenuItem loginItem = menu.findItem(R.id.action_login);
User u = sharedPreference.getUser(BaseActivity.this);
if (u.isLogged()) {
loginItem.setVisible(false);
}
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_login) {
if (loggedUser.isLogged())//
{
builder = new AlertDialog.Builder(getApplicationContext());
builder.setMessage(menuDialogMessage)
.setTitle(menuDialogTitle)
.setCancelable(false);
builder.setPositiveButton(ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(getApplicationContext(), LoginActivity.class));
dialog.dismiss();
}
});
builder.setNegativeButton(cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog = builder.create();
dialog.show();
return true;
} else {
startActivity(new Intent(getApplicationContext(), LoginActivity.class));
return true;
}
} else if (id == R.id.action_settings) {
builder = new AlertDialog.Builder(getApplicationContext());
builder.setMessage(menuDialogMessage)
.setTitle(menuDialogTitle)
.setCancelable(false);
builder.setPositiveButton(ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(getApplicationContext(), Settings.class));
dialog.dismiss();
}
});
builder.setNegativeButton(cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog = builder.create();
dialog.show();
return true;
} else if (id == R.id.action_change_pw) {
builder = new AlertDialog.Builder(getApplicationContext());
builder.setMessage(menuDialogMessage)
.setTitle(menuDialogTitle)
.setCancelable(false);
builder.setPositiveButton(ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(getApplicationContext(), ChangePassword.class));
dialog.dismiss();
}
});
builder.setNegativeButton(cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog = builder.create();
dialog.show();
return true;
} else if (id == R.id.action_logout) {
builder = new AlertDialog.Builder(getApplicationContext());
builder.setMessage(menuDialogMessage)
.setTitle(menuDialogTitle)
.setCancelable(false);
builder.setPositiveButton(ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
sharedPreference.removeUser(BaseActivity.this);
startActivity(new Intent(getApplicationContext(), LoginActivity.class));
dialog.dismiss();
}
});
builder.setNegativeButton(cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog = builder.create();
dialog.show();
return true;
} else if (id == R.id.action_exit) {
builder = new AlertDialog.Builder(BaseActivity.this);
builder.setMessage(exit)
.setTitle(menuDialogTitle)
.setCancelable(false);
builder.setPositiveButton(ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
Globals.mContext.finish();
System.exit(1);
dialog.dismiss();
}
});
builder.setNegativeButton(cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
dialog = builder.create();
dialog.show();
return true;
}
return super.onOptionsItemSelected(item);
}
You can write a base activity which implements these methods. All your activities can extend this base activity instead of AppCompatActivity. This way they will all have this menu 'by default'.