Example usage for android.app Fragment setHasOptionsMenu

List of usage examples for android.app Fragment setHasOptionsMenu

Introduction

In this page you can find the example usage for android.app Fragment setHasOptionsMenu.

Prototype

public void setHasOptionsMenu(boolean hasMenu) 

Source Link

Document

Report that this fragment would like to participate in populating the options menu by receiving a call to #onCreateOptionsMenu and related methods.

Usage

From source file:com.abewy.android.apps.klyph.app.MainActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /*//from w ww . ja  v  a 2  s .co  m
     * if (KlyphSession.getSessionUserName() != null)
     * {
     * loggedIn = true;
     * setTitle(KlyphSession.getSessionUserName());
     * }
     * else
     * {
     * if (KlyphFlags.IS_PRO_VERSION == true)
     * setTitle(R.string.app_pro_name);
     * else
     * setTitle(R.string.app_name);
     * }
     */
    setTitle("");

    if (Session.getActiveSession() == null || KlyphSession.getSessionUserId() == null
            || (Session.getActiveSession() != null && Session.getActiveSession().isOpened() == false)) {
        getActionBar().hide();
        getFragmentManager().beginTransaction().add(R.id.main, new LoginFragment(), FRAGMENT_TAG).commit();
    }

    // notificationsFragment.setHasOptionsMenu(false);
    adContainer = (ViewGroup) findViewById(R.id.ad);

    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
    drawerToggle = new ActionBarDrawerToggle(this, drawer, AttrUtil.getResourceId(this, R.attr.drawerIcon),
            R.string.open, R.string.close) {
        @Override
        public void onDrawerOpened(View view) {
            Log.d("MainActivity.onCreate(...).new ActionBarDrawerToggle() {...}", "onDrawerOpened: ");
            super.onDrawerOpened(view);

            Fragment fragment = getFragmentManager().findFragmentByTag(FRAGMENT_TAG);

            if (drawer.isDrawerOpen(Gravity.RIGHT)) {
                // drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN, Gravity.RIGHT);

                if (notificationsFragment != null) {
                    notificationsFragment.setHasOptionsMenu(true);
                    notificationsFragment.onOpenPane();
                }

                if (fragment != null)
                    fragment.setHasOptionsMenu(false);
            } else if (drawer.isDrawerOpen(Gravity.LEFT)) {

                if (notificationsFragment != null) {
                    notificationsFragment.setHasOptionsMenu(false);
                }

                if (fragment != null)
                    fragment.setHasOptionsMenu(true);
            }

            invalidateOptionsMenu();
        }

        @Override
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);

            drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, Gravity.RIGHT);

            if (!drawer.isDrawerOpen(Gravity.RIGHT)) {
                if (notificationsFragment != null)
                    notificationsFragment.setHasOptionsMenu(false);

                Fragment fragment = getFragmentManager().findFragmentByTag(FRAGMENT_TAG);

                if (fragment != null)
                    fragment.setHasOptionsMenu(true);
            }
            invalidateOptionsMenu();
        }
    };

    drawer.setDrawerListener(drawerToggle);

    final List<String> labels = KlyphPreferences.getLeftDrawerMenuLabels();
    classes = new ArrayList<String>(KlyphPreferences.getLeftDrawerMenuClasses());
    classes.add("com.abewy.android.apps.klyph.fragment.UserTimeline");
    navAdapter = new DrawerLayoutAdapter(getActionBar().getThemedContext(), R.layout.item_drawer_layout,
            labels);

    final ListView navList = (ListView) findViewById(R.id.drawer);

    // Setting drawers max width
    int maxWidth = getResources().getDimensionPixelSize(R.dimen.max_drawer_layout_width);

    int w = Math.min(KlyphDevice.getDeviceWidth(), KlyphDevice.getDeviceHeight())
            - getResources().getDimensionPixelSize(R.dimen.dip_64);

    int finalWidth = Math.min(maxWidth, w);

    LayoutParams params = ((View) navList.getParent()).getLayoutParams();
    params.width = finalWidth;
    ((View) navList.getParent()).setLayoutParams(params);

    final View notificationContainer = findViewById(R.id.notifications_container);
    params = notificationContainer.getLayoutParams();
    params.width = finalWidth;
    notificationContainer.setLayoutParams(params);

    // End max width
    navList.setFadingEdgeLength(0);
    navList.setVerticalFadingEdgeEnabled(false);
    navList.setAdapter(navAdapter);

    navList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int pos, long id) {
            updateContent(pos);
            drawer.closeDrawer(Gravity.LEFT);
        }
    });

    // Try to use more data here. ANDROID_ID is a single point of attack.
    // String deviceId = Secure.getString(getContentResolver(), Secure.ANDROID_ID);

    // Library calls this when it's done.
    // mLicenseCheckerCallback = new MyLicenseCheckerCallback();

    // Construct the LicenseChecker with a policy.
    // mChecker = new LicenseChecker(this, new ServerManagedPolicy(this, new AESObfuscator(SALT, getPackageName(), deviceId)), BASE64_PUBLIC_KEY);

    // mChecker.checkAccess(mLicenseCheckerCallback)

    // Facebook HashKey
    if (KlyphFlags.LOG_FACEBOOK_HASH)
        FacebookUtil.logHash(this);

    // Hierarchy View Connector
    if (KlyphFlags.ENABLE_HIERACHY_VIEW_CONNECTOR)
        HierachyViewUtil.connectHierarchyView(this);
}

From source file:com.abewy.android.apps.openklyph.app.MainActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /*// ww  w .  ja va 2 s  .c o m
     * if (KlyphSession.getSessionUserName() != null)
     * {
     * loggedIn = true;
     * setTitle(KlyphSession.getSessionUserName());
     * }
     * else
     * {
     * if (KlyphFlags.IS_PRO_VERSION == true)
     * setTitle(R.string.app_pro_name);
     * else
     * setTitle(R.string.app_name);
     * }
     */
    setTitle("");

    if (Session.getActiveSession() == null || KlyphSession.getSessionUserId() == null
            || (Session.getActiveSession() != null && Session.getActiveSession().isOpened() == false)) {
        getActionBar().hide();
        getFragmentManager().beginTransaction().add(R.id.main, new LoginFragment(), FRAGMENT_TAG).commit();
    }

    // notificationsFragment.setHasOptionsMenu(false);
    adContainer = (ViewGroup) findViewById(R.id.ad);

    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
    drawerToggle = new ActionBarDrawerToggle(this, drawer, AttrUtil.getResourceId(this, R.attr.drawerIcon),
            R.string.open, R.string.close) {
        @Override
        public void onDrawerOpened(View view) {
            Log.d("MainActivity.onCreate(...).new ActionBarDrawerToggle() {...}", "onDrawerOpened: ");
            super.onDrawerOpened(view);

            Fragment fragment = getFragmentManager().findFragmentByTag(FRAGMENT_TAG);

            if (drawer.isDrawerOpen(Gravity.RIGHT)) {
                // drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN, Gravity.RIGHT);

                if (notificationsFragment != null) {
                    notificationsFragment.setHasOptionsMenu(true);
                    notificationsFragment.onOpenPane();
                }

                if (fragment != null)
                    fragment.setHasOptionsMenu(false);
            } else if (drawer.isDrawerOpen(Gravity.LEFT)) {

                if (notificationsFragment != null) {
                    notificationsFragment.setHasOptionsMenu(false);
                }

                if (fragment != null)
                    fragment.setHasOptionsMenu(true);
            }

            invalidateOptionsMenu();
        }

        @Override
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);

            drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, Gravity.RIGHT);

            if (!drawer.isDrawerOpen(Gravity.RIGHT)) {
                if (notificationsFragment != null)
                    notificationsFragment.setHasOptionsMenu(false);

                Fragment fragment = getFragmentManager().findFragmentByTag(FRAGMENT_TAG);

                if (fragment != null)
                    fragment.setHasOptionsMenu(true);
            }
            invalidateOptionsMenu();
        }
    };

    drawer.setDrawerListener(drawerToggle);

    final List<String> labels = KlyphPreferences.getLeftDrawerMenuLabels();
    classes = new ArrayList<String>(KlyphPreferences.getLeftDrawerMenuClasses());
    classes.add("com.abewy.android.apps.openklyph.fragment.UserTimeline");
    navAdapter = new DrawerLayoutAdapter(getActionBar().getThemedContext(), R.layout.item_drawer_layout,
            labels);

    final ListView navList = (ListView) findViewById(R.id.drawer);

    // Setting drawers max width
    int maxWidth = getResources().getDimensionPixelSize(R.dimen.max_drawer_layout_width);

    int w = Math.min(KlyphDevice.getDeviceWidth(), KlyphDevice.getDeviceHeight())
            - getResources().getDimensionPixelSize(R.dimen.dip_64);

    int finalWidth = Math.min(maxWidth, w);

    LayoutParams params = ((View) navList.getParent()).getLayoutParams();
    params.width = finalWidth;
    ((View) navList.getParent()).setLayoutParams(params);

    final View notificationContainer = findViewById(R.id.notifications_container);
    params = notificationContainer.getLayoutParams();
    params.width = finalWidth;
    notificationContainer.setLayoutParams(params);

    // End max width
    navList.setFadingEdgeLength(0);
    navList.setVerticalFadingEdgeEnabled(false);
    navList.setAdapter(navAdapter);

    navList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int pos, long id) {
            updateContent(pos);
            drawer.closeDrawer(Gravity.LEFT);
        }
    });

    // Try to use more data here. ANDROID_ID is a single point of attack.
    // String deviceId = Secure.getString(getContentResolver(), Secure.ANDROID_ID);

    // Library calls this when it's done.
    // mLicenseCheckerCallback = new MyLicenseCheckerCallback();

    // Construct the LicenseChecker with a policy.
    // mChecker = new LicenseChecker(this, new ServerManagedPolicy(this, new AESObfuscator(SALT, getPackageName(), deviceId)), BASE64_PUBLIC_KEY);

    // mChecker.checkAccess(mLicenseCheckerCallback)

    // Facebook HashKey
    if (KlyphFlags.LOG_FACEBOOK_HASH)
        FacebookUtil.logHash(this);

    // Hierarchy View Connector
    if (KlyphFlags.ENABLE_HIERACHY_VIEW_CONNECTOR)
        HierachyViewUtil.connectHierarchyView(this);
}