Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include common code in android

Tags:

android

I am creating am android application which has 4 different activities all having a common menu. To show menu in all activities generally I need to add this code in every files.

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}

Is there anyway that I can add this code in one single file & include to all activities?

Thanks in advance.

like image 479
Zahid Habib Avatar asked Dec 27 '22 08:12

Zahid Habib


1 Answers

Create one Main Activity write your menu code in that activity and then extend other activities with Main Activity..

public MainActivity extends Activity 
{ 
  @Override 
   public boolean onCreateOptionsMenu(Menu menu) { 
   MenuInflater inflater = getMenuInflater(); 
   inflater.inflate(R.menu.menu, menu); 
   return true; 
} 

   @Override 
   public boolean onOptionsItemSelected(MenuItem item) { 
           return false; 
   }     
} 

And some TempActivity

public TempActivity extend MainActivity
{

/......
}
like image 171
user370305 Avatar answered Jan 14 '23 00:01

user370305