Adding the same context menu to multiple activities

折月煮酒 提交于 2019-11-29 04:05:48

if you want to add same functionality in more than 1 activity than create 1 common activity like BaseActivity and extend that activity will include that common functions in your inherited all activities

for example i have called checklogin function , you can put your menu code here,

public class BaseActivity extends Activity {

      @Override
     protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     settings = getSharedPreferences(PREFS_NAME, 0);
        if (IsFullScreen) {
           requestWindowFeature(Window.FEATURE_NO_TITLE);
           getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }

        this.CheckLogin();
     }

    // Check login function
    // Your menu code

  }

now you can extend it in your activities

public class MainScreen extends BaseActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    this.setContentView(R.layout.mainscreen);

   }

}

You can define a menu in an xml file and then load the menu in onCreateOptionsMenu. You will still need to handle each menu item in each activity. You could also create a BaseActivity class that handles the menu stuff that each Activity could extend.

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/about" android:title="About"
        android:icon="@drawable/ic_menu_about"/>
    <item android:id="@+id/search"
          android:icon="@drawable/ic_menu_search" android:title="Search"></item>
    <item android:id="@+id/my_location"
          android:title="My Location"
          android:icon="@drawable/ic_menu_mylocation">
    </item>
</menu>


public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

Try to use a abstract class

  abstract class BaseMenu extends Activity
  {
         //Initialize your menus

         // bottom menus
public static final int Menu1 = 1;
public static final int Menu2 = 2;
public static final int Menu3 = 3;
public static final int Menu4 = 4;
public static final int Menu5 = 5;
public static final int Menu6 = 6;
public static final int Menu7 = 7;


// / Creates the menu items
public boolean onCreateOptionsMenu(Menu menu) {

    menu.add(0, Menu3, 0, "Create Profile").setIcon(
            this.getResources().getDrawable(R.drawable.ic_menu_add));
    menu.add(0, Menu5, 0, "Log In").setIcon(
            this.getResources().getDrawable(R.drawable.ic_menu_login));
    menu.add(0, Menu2, 0, "Settings").setIcon(
            this.getResources().getDrawable(R.drawable.ic_menu_preferences));
    menu.add(0, Menu4, 0, "About").setIcon(
            this.getResources().getDrawable(R.drawable.ic_menu_help));
    menu.add(0, Menu1, 0, "Report A Bug").setIcon(
            this.getResources().getDrawable(R.drawable.ic_menu_start_conversation));
    menu.add(0, Menu6, 0, "New Stuff").setIcon(
            this.getResources().getDrawable(R.drawable.ic_menu_view));
    return true;
}



private MenuItem add(int i, int menu32, int j, String string) {
    // TODO Auto-generated method stub
    return null;
}



// Handles item selections from preference menu
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case Menu1:
        startActivity(new Intent(this, Bug.class));
        return true;
    case Menu2:
        startActivity(new Intent(this, EditPreferences.class));
        return true;
    case Menu3:
        startActivity(new Intent(this, CreateAccount.class));
        return true;
    case Menu4:
        startActivity(new Intent(this, About.class));
        return true;
    case Menu5:
        startActivity(new Intent(this, Login.class));
        return true;
    case Menu6:
        startActivity(new Intent(this, NewAdditions.class));
        return true;
    }

    return false;
}}

Now extend the class BaseMenu instead of Activity

I Think this could help you out.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!