I want to use switch button in navigation drawer for adding and removing fragment from main layout.
this my code- menuitem.xml`
<group
android:id="@+id/drawer_group1"
android:checkableBehavior="single">
<item
android:id="@+id/nav_timer"
android:icon="@drawable/ic_timer"
android:title="Timer">
</item>
<item
android:id="@+id/addFragment_Bt"
app:actionViewClass="android.widget.Switch"
android:title="Most Used" />
<item
android:id="@+id/nav_settings"
android:icon="@drawable/ic_settings"
android:title="Settings">
</item>
</group>`
MainActivity.class
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
switch (menuItem.getItemId()) {
case R.id.addFragment_Bt:
Switch switchCompat = findViewById(R.id.addMostUsed_Bt);
switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment fragment = fragmentManager.findFragmentById(R.id.Most_Used_Fragment_container);
if (isChecked == true) {
if (fragment != null) {
fragmentManager.popBackStack();
}
}
}
});
break;
}
}
mDrawerLayout.closeDrawer(GravityCompat.START);
return true;
}
}
for now i am just trying to remove already added fragment.