0

I am trying to hide NavigationDrawer and ActionBar from fragment.

I have created the following interface and implemented the same in activity

  public interface CommonMethodsListener {
      void hideActionbar();
      void showActionbar();
      void setActionBarTitle(String title);
  }

I have implemented following methods in Activity.

  public void setDrawerEnabled(boolean enabled) {
      int lockMode = enabled ? DrawerLayout.LOCK_MODE_UNLOCKED :
              DrawerLayout.LOCK_MODE_LOCKED_CLOSED;
      drawer.setDrawerLockMode(lockMode);
      toggle.setDrawerIndicatorEnabled(enabled);
  }

  @Override
  public void hideActionbar() {
      getSupportActionBar().hide();
      setDrawerEnabled(false);
  }

  @Override
  public void showActionbar() {
      getSupportActionBar().show();
      setDrawerEnabled(true);
  }

I am calling the hideActionBar() and showActionBar() in Fragment as below

  @Override
  public void onResume() {
      super.onResume();
      mCommonMethodsListener.hideActionbar();
  }

  @Override
  public void onPause() {
      super.onPause();
      mCommonMethodsListener.showActionbar();
  }

I am having trouble hiding the ActionBar and NavigationDrawer with the above code.

I have two fragments called SplashFragment and LoginFragment. I am calling the hideActionBar() and 'showActionBar()inonResume()andonPause()` in both fragments.

The SplashFragment loads without ActionBar and NavigationDrawer. An Handler with postDelayed will navigation to LoginFragment after a preset delay of 3000 mSec.

The LoginFragment shows up with ActionBar and NavigationDrawer.

The build.gradle is as below.

  android {
      compileSdkVersion 28
      defaultConfig {
          applicationId "io.azpire.ncloudpay"
          minSdkVersion 24
          targetSdkVersion 28
          versionCode 1
          versionName "1.0"
          testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
      }
      buildTypes {
          release {
              minifyEnabled false
              proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
          }
      }
      dataBinding {
          enabled = true
      }
  }

  dependencies {
      implementation fileTree(dir: 'libs', include: ['*.jar'])
      implementation 'androidx.appcompat:appcompat:1.0.2'
      implementation 'androidx.legacy:legacy-support-v4:1.0.0'
      implementation 'com.google.android.material:material:1.0.0'
      implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
      implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
      testImplementation 'junit:junit:4.12'
      androidTestImplementation 'androidx.test:runner:1.1.1'
      androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
      implementation 'androidx.navigation:navigation-fragment:2.0.0'
      implementation 'androidx.navigation:navigation-ui:2.0.0'

      implementation "androidx.room:room-runtime:2.1.0-beta01"
      annotationProcessor "androidx.room:room-compiler:2.1.0-beta01"
  }
gv1507
  • 47
  • 4
  • 10
  • Possible duplicate of [How to hide navigation drawer when opening certain fragment?](https://stackoverflow.com/questions/38432973/how-to-hide-navigation-drawer-when-opening-certain-fragment) – YQadoome May 25 '19 at 07:37
  • @YQadoome The code I have shared does work on loading of `SplashFragment`, but does not work for nextfragment. I am using `Navigation` component from `Architecture Components` for replacing the `SplashFragment` with `LoginFragment` – gv1507 May 26 '19 at 02:45

2 Answers2

0

You should change your calling method in fragment to

onResume() -> onCreate()

onPause() -> onDestroy()

Both this method is called once, when fragment attached to activity

  • I am not sure `onCreate()` and `onDestroy()` are right places hide and show. But, having hide and show in `onCreate()` and `onDestroy()` does not work. – gv1507 May 24 '19 at 12:33
  • Are you doing this in fragment? – Ankit ihelper Sharma May 24 '19 at 12:34
  • supppose, you want to show action bar or hide navigatin drawer for fragment, then just simply define operation in onCreate or onDestroy in Fragment will be help you. When second fragment open, you can define default setting for show or hide for actionbar and navigation drawer – Ankit ihelper Sharma May 24 '19 at 19:14
0

Change

mCommonMethodsListener.hideActionbar();
mCommonMethodsListener.setDrawerEnabled(False);

to

((Activity)getActivity()).hideActionbar();
((Activity)getActivity()).setDrawerEnabled(False);

and put them in OnCreate()

YQadoome
  • 178
  • 14
  • I tried, but no change. The code I have shared does work on loading of SplashFragment, but does not work for nextfragment. I am using Navigation component from Architecture Components for replacing the SplashFragment with LoginFragment – gv1507 May 26 '19 at 02:46