See Action Bar in All Actions - Android

In my main activity, I have an action bar. How can this be seen even if I start a new activity?

Should a new activity expand my core business for this?

+6
source share
3 answers

If you declare the onCreateOptionMenu method, which is where you put items in the action bar, in your main action (A), all other actions that extend A without re-declaring this method will have the same action bar A.

+6
source

Android menu options provide the user with actions and other options for selecting from the action bar on the screen. Some of these actions are common to all actions for your application, so instead of creating them in each of your actions, you can create BaseActivity, which extends the Activity class and performs all your menu processing. You can then extend the base activity class in your application actions to get the same menu options.

 import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends BaseActivity implements OnClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button nextActivity = (Button) findViewById(R.id.nextActivity); nextActivity.setOnClickListener(this); } } 

Here is the BaseActivity class

 import android.app.Activity; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast; public class BaseActivity extends Activity{ @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.common_menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { // Do Code Here default: return super.onOptionsItemSelected(item); } } } 

Hope this helps you.

+3
source

You can use the same implementation pattern (inheritance / composition) that I described here.

Global search function of the entire application

for the search function. Just do what I described there using the onCreateOptionMenu method to have the same thing for all actions without having to write the same three lines of code in each action.

0
source

Source: https://habr.com/ru/post/921951/


All Articles