I am using a Navigation drawer in my Main screen and use the third party library com.mikepenz:materialdrawer:5.1.6#aar, to build my Material Drawer. My requirement is that I want to hide some menu items based on a condition. Is there a way to achieve it? This is how I create my drawer.
private Drawer result = null;
AccountHeader headerResult;
final PrimaryDrawerItem home = new PrimaryDrawerItem().withName("Home").withIdentifier(1).withIcon(GoogleMaterial.Icon.gmd_home);
final PrimaryDrawerItem profile = new PrimaryDrawerItem().withName("Profile").withIdentifier(2).withIcon(GoogleMaterial.Icon.gmd_account);
final PrimaryDrawerItem gallery = new PrimaryDrawerItem().withName("Gallery").withIdentifier(3).withIcon(R.drawable.ic_perm_media_black_24dp);
final PrimaryDrawerItem recognition = new PrimaryDrawerItem().withName("Recognition").withIdentifier(4).withIcon(GoogleMaterial.Icon.gmd_face);
final PrimaryDrawerItem maps = new PrimaryDrawerItem().withName("Maps").withIdentifier(5).withIcon(R.drawable.ic_place_black_24dp);
final PrimaryDrawerItem tagAndLocate = new PrimaryDrawerItem().withName("Tag & Locate").withIdentifier(6).withIcon(R.drawable.ic_remove_red_eye_black_24dp);
final PrimaryDrawerItem gamesAndPuzzle = new PrimaryDrawerItem().withName("Games & Puzzles").withIdentifier(7).withIcon(R.drawable.ic_casino_black_24dp);
final PrimaryDrawerItem backup = new PrimaryDrawerItem().withName("Backup").withIdentifier(8).withIcon(GoogleMaterial.Icon.gmd_save);
final PrimaryDrawerItem logout = new PrimaryDrawerItem().withName("Logout").withIdentifier(9).withIcon(FontAwesome.Icon.faw_sign_out);
headerResult = new AccountHeaderBuilder()
.withActivity(this)
.withHeaderBackground(R.drawable.header)
.withSelectionListEnabledForSingleProfile(false)
.addProfiles(userProfile)
.withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
#Override
public boolean onProfileChanged(View view, IProfile profile, boolean currentProfile) {
return false;
}
})
.build();
result = new DrawerBuilder()
.withActivity(this)
.withAccountHeader(headerResult)
.withToolbar(toolbar)
.withDisplayBelowStatusBar(false)
.withTranslucentStatusBar(true)
.withSavedInstance(savedInstanceState)
.withActionBarDrawerToggle(true)
.withActionBarDrawerToggleAnimated(true)
.addDrawerItems(home)
.addDrawerItems(profile)
.addDrawerItems(gallery)
.addDrawerItems(recognition)
.addDrawerItems(maps)
.addDrawerItems(tagAndLocate)
.addDrawerItems(gamesAndPuzzle)
.addDrawerItems(backup)
.addDrawerItems(new DividerDrawerItem())
.addDrawerItems(logout)
.buildForFragment();
resultStandard.setOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
#Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
int drawItemId = (int) drawerItem.getIdentifier();
Intent intent;
Fragment fragment;
switch (drawItemId) {
case 1:
fragment = new HomeFragment();
gaFragmentStack.add(home);
break;
case 2:
fragment = new ProfileFragment();
gaFragmentStack.add(profile);
break;
case 3:
fragment = new GalleryFragment();
gaFragmentStack.add(gallery);
break;
case 4:
fragment = new RecognitionFragment();
gaFragmentStack.add(recognition);
break;
case 5:
fragment = new MapsFragment();
gaFragmentStack.add(maps);
break;
case 6:
fragment = new TagLocateFragment();
gaFragmentStack.add(tagAndLocate);
break;
case 7:
fragment = new GamesPuzzlesFragment();
gaFragmentStack.add(gamesAndPuzzle);
break;
case 8:
fragment = new BackupFragment();
gaFragmentStack.add(backup);
break;
default:
fragment = new HomeFragment();
break;
}
if (drawItemId == 9) {
FirebaseAuth.getInstance().signOut();
SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.apply();
intent = new Intent(MainMenuActivity.this, SplashScreen.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container_gaFragments, fragment);
transaction.commit();
return false;
}
});
I just want to hide some Drawer item based on a condition. Is there a way to do that? Any help is appreciated.
There is no way to hide a particular item from the drawer. You can resolve this by removing all items (removeAllItems() and removeAllStickyFooterItems()) and adding them selectively again. Below is the example code for Kotlin where you can handle this with IF statement inside the slider.apply {...}
fun refreshMenuItems() {
slider.apply {
removeAllItems()
removeAllStickyFooterItems()
if (isLoggedUser == false) {
addItems(
PrimaryDrawerItem().apply {
nameText = "Login"
iconicsIcon = GoogleMaterial.Icon.gmd_verified_user
identifier = 70
},
DividerDrawerItem(),
PrimaryDrawerItem().apply {
nameText = "Register"
iconicsIcon = GoogleMaterial.Icon.gmd_how_to_reg
identifier = 80
}
)
} else {
addItems(
PrimaryDrawerItem().apply {
nameText = "My profile"
iconicsIcon = GoogleMaterial.Icon.gmd_verified_user
identifier = 70
},
DividerDrawerItem(),
PrimaryDrawerItem().apply {
nameText = "My reservations"
iconicsIcon = GoogleMaterial.Icon.gmd_how_to_reg
identifier = 80
}
)
addStickyDrawerItems(
PrimaryDrawerItem().apply {
nameText = "Log out"
isSelectable = false
identifier = 100
}
)
}
}
}
For example, you can call this method from onResume() method
override fun onResume() {
super.onResume()
changeDrawerUserName()
refreshMenuItems()
}
Related
Within my navigation drawer, I am trying to execute a code to start a new activity so it moves from one activity to another. However, When I use a DividerDrawerItem() it does not execute the last case statement (4). I've tried it without the DividerDrawerItem() and it works but when I add it in, it does not.
PrimaryDrawerItem item1 = new PrimaryDrawerItem().withIdentifier(1).withName("Home Page");
PrimaryDrawerItem item2 = new PrimaryDrawerItem().withIdentifier(2).withName("Rank Table");
PrimaryDrawerItem item3 = new PrimaryDrawerItem().withIdentifier(3).withName("Report");
PrimaryDrawerItem item4 = new PrimaryDrawerItem().withIdentifier(4).withName("Log out");
//create the drawer and remember the `Drawer` result object
Drawer result = new DrawerBuilder()
.withActivity(this)
.withAccountHeader(headerResult)
.withToolbar(toolbar)
.addDrawerItems(
item1, item2,item3,
new DividerDrawerItem(),
item4
)
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
#Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
// do something with the clicked item :D
switch(position){
case 1: startActivity(new Intent(HomePage.this, HomePage.class));
break;
case 2: startActivity(new Intent(HomePage.this, RankT.class));
break;
case 3: startActivity(new Intent(HomePage.this,Report.class));
break;
case 4: firebaseAuth.signOut();
finish();
startActivity(new Intent(HomePage.this,MainActivity.class));
}
return true;
}
I think your code is using position where it should use identifier.
Change switch(position) to switch((int) drawerItem.getIdentifier())
I will try to explain at best, adding references and images in which
I think it is difficult to explain in words the error I encountered.
Working with the fragments and the Navigation Drawer I've faced this
bug:
From time to time, when I open the navigation menu, the previous
fragment is duplicated or loaded again. This thing is completely
random, without a precise scheme. The only thing that seems
connected is the RecyclerView and the Navigation Drawer.
Just because I do not know where I'm wrong, write in the comments if you
need something, code or other information and from time to time I'll add it
to the original question.
I am not receiving any errors and to help both me and you add some code and
images of how that fragment is loaded without bugs:
#Override
public boolean onNavigationItemSelected(MenuItem item) {
Fragment fragment = null;
Class aclass = null;
Bundle args = new Bundle();
Intent intentOperatore = getIntent();
String mParamOperatore = intentOperatore.getStringExtra(ARG_PARAM_OPERATORE);
boolean navigation = Boolean.FALSE;
boolean intent = Boolean.FALSE;
boolean scan = Boolean.FALSE;
boolean exit = Boolean.FALSE;
try{
switch (item.getItemId()){
case R.id.nav_home:
aclass = HomeFragment.class;
fragment = (Fragment) aclass.newInstance();
args.putString(ARG_PARAM_OPERATORE,mParamOperatore);
fragment.setArguments(args);
navigation = Boolean.TRUE;
break;
case R.id.nav_procedura_immobiliare:
aclass = FiltroFragment.class;
fragment = (Fragment) aclass.newInstance();
args.putString(ARG_CHECK_CLASS,"immobiliari");
fragment.setArguments(args);
navigation = Boolean.TRUE;
break;
case R.id.nav_procedura_mobiliare:
aclass = FiltroFragment.class;
fragment = (Fragment) aclass.newInstance();
args.putString(ARG_CHECK_CLASS,"mobiliari");
fragment.setArguments(args);
navigation = Boolean.TRUE;
break;
case R.id.nav_localizza_procedura:
aclass = LocalizzaActivity.class;
intent = Boolean.TRUE;
break;
case R.id.nav_ricognizione:
aclass = RicognizioneFragment.class;
fragment = (Fragment) aclass.newInstance();
args.putString(ARG_PARAM_RICOGNIZIONE,""); //TODO: Add param here.
fragment.setArguments(args);
navigation = Boolean.TRUE;
break;
case R.id.nav_qrcode:
scan = Boolean.TRUE;
break;
case R.id.nav_exit:
exit = Boolean.TRUE;
break;
}
if(navigation) { // Navigazione per i fragment
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
} if(intent) { // Navigazione per le activity
Intent myIntent = new Intent(getApplicationContext(),aclass);
myIntent.putExtra(ARG_PARAM_OPERATORE,mParamOperatore);
startActivity(myIntent);
finish();
} if(scan) {
new MVBarcodeScanner.Builder()
.setScanningMode(MVBarcodeScanner.ScanningMode.SINGLE_AUTO)
.setFormats(Barcode.QR_CODE)
.build()
.launchScanner(MainActivity.this,REQ_CODE);
} if(exit) { // Navigazione per l'uscita
MaterialDialog dialog = new MaterialDialog.Builder(this)
.title(R.string.title_exit)
.content(R.string.summary_exit)
.positiveText(R.string.action_exit)
.onPositive(new MaterialDialog.SingleButtonCallback() {
#Override
public void onClick(MaterialDialog dialog, DialogAction which) {
finish();
}
})
.negativeText(R.string.action_no)
.show();
}
} catch (Exception e) {
Log.e(this.getClass().getName(), e.getMessage(), e);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Edit:
I also notice another thing that recycling is not the only thing to
duplicate. There is also the toolbar item that is duplicated:
Edit 2:
- I've added the onCreateOptionsMenu and some code that i run in the
onCreateView:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
//inflater.inflate(R.menu.procedura_menu_action, menu);
inflater.inflate(R.menu.procedura_menu_list, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_procedure_list, container, false);
Context context = view.getContext();
setHasOptionsMenu(true);
recyclerView = (RecyclerView) view.findViewById(R.id.rvProcedure);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(context);
mAdapter = new ProcedureAdapter(context,procedure,this);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new DividerItemDecoration(context,LinearLayoutManager.VERTICAL));
recyclerView.setAdapter(mAdapter);
actionModeCallback = new ActionModeCallback();
Toolbar toolbar = view.findViewById(R.id.toolbar);
toolbar.setTitle(getString(R.string.title_lista_procedure) + " " + mCheckClass);
toolbar.setNavigationIcon(R.drawable.ic_menu_navigator);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
toolbar.setNavigationOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
DrawerLayout drawerLayout = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout);
if(drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
} else {
drawerLayout.openDrawer(GravityCompat.START);
}
}
});
floatingActionButton = (FloatingActionButton) view.findViewById(R.id.fab_new);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
args.putString(ARG_PARAM_TIPOLOGIA,mParamTipologia); //Passa la procedura selezionata se siamo in modifica
args.putString(ARG_PARAM_PROCEDURA,new Gson().toJson(procedure.get(lastPosition)));
if(actionMode != null) //Leva la selezione se รจ presente, prima di passare ad un'altro fragment.
enableActionMode(lastPosition);
try {
if ((getActivity()) != null) {
FragmentManager fragmentManager = ((AppCompatActivity)getActivity()).getSupportFragmentManager();
Class fragmentClass = ProceduraFragment.class;
Fragment fragment = (Fragment) fragmentClass.newInstance();
fragment.setArguments(args);
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).addToBackStack(null).commit();
}
} catch (java.lang.InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
}
});
//TODO: Implements here the load logic for procedure.
if(mParamProcedura != null && !(procedure.size() > 0)) {
procedure.add(mParamProcedura);
} else if(!(procedure.size() > 0)) {
refreshList(AlphaMAS.getData());
}
return view;
}
Why create a new fragment instance everytime? Try something like this:
if (fragment == null)
fragment = (Fragment) aclass.newInstance();
Inside your layout in both Fragments use this :
android:clickable="true"
android:focusable="true"
Solution
Set background to both of your xml file's parent layout.
android:background="#color/background_color_which_you_want"
Edit 1:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.procedura_menu_list, menu);
return super.onCreateOptionsMenu(menu);
}
I have a bottom activity with 3 menus (Fragment). Home , News and Settings. In Setting there is another menus (ListView) . Profil, About and Contact Us.
If you click Profil it will open new activity (Profil.Class) .
My question is how can I open recent tab (Setting), because now if I click back button and back arrow from toolbar it will open My bottom activity with home fragment instead of settings framgent. I trying to send putExtra(); from my SettingFragment but its not working . how can I achieve that?
So here is my MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = getSupportActionBar();
BottomNavigationView navigation = findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
Intent intent = getIntent();
Integer tabtoOpen = intent.getIntExtra("Tab",0);
Log.d("TABB", tabtoOpen.toString());
if(tabtoOpen == 0){
fragment = new MainFragment();
}else if(tabtoOpen == 1){
fragment = new NewsFragment();
}else if(tabtoOpen == 2){
fragment = new SettingsFragment();
}
loadFragment(fragment);
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
toolbar.setTitle("Home | " +getString(R.string.app_name));
fragment = new MainFragment();
loadFragment(fragment);
return true;
case R.id.navigation_news:
toolbar.setTitle("News | " +getString(R.string.app_name));
fragment = new NewsFragment();
loadFragment(fragment);
return true;
case R.id.navigation_setting:
fragment = new SettingsFragment();
loadFragment(fragment);
toolbar.setTitle("Setting | " +getString(R.string.app_name));
return true;
}
return false;
}
};
private void loadFragment(Fragment fragment) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
and here is my profil
public void onBackPressed() {
Intent i = new Intent(this, Activity_main.class);
i.putExtra("Tab", 2);
this.startActivity(i);
}
And you can set whichever navigation item you'd like to be selected using setSelectedItemId().
else if(tabtoOpen == 2){
navigation.setSelectedItem(R.id.navigation_setting);
}
I currently have an existing project in which I would like to implement an navigation drawer activity. Currently, if I add a new navigation drawer activity, it generates the following layout:
However, instead of the layout above I would like my navigation drawer to look exactly like the default template one which looks like this (consisting of a nav header, etc):
How can I go about achieving this? I'm new to Android development.
EDIT: I just needed to change my app theme to achieve what I wanted
Create a new Drawer Activity :
File->new->activity->Navigation Drawer Activity
private void setupNavigationDrawer() {
//if you want to update the items at a later time it is recommended to keep it in a variable
PrimaryDrawerItem item1 = new PrimaryDrawerItem().withIdentifier(0).withName(R.string.drawer_item_home).withIcon(getResources().getDrawable(R.drawable.ic_home_white_24dp));
PrimaryDrawerItem item2 = new PrimaryDrawerItem().withIdentifier(1).withName(R.string.drawer_item_login).withIcon(getResources().getDrawable(R.drawable.ic_account_circle_white_24dp));
PrimaryDrawerItem item3 = new PrimaryDrawerItem().withIdentifier(2).withName(R.string.drawer_item_movies).withIcon(getResources().getDrawable(R.drawable.ic_movie_white_24dp));
PrimaryDrawerItem item4 = new PrimaryDrawerItem().withIdentifier(3).withName(R.string.drawer_item_trailers).withIcon(getResources().getDrawable(R.drawable.ic_videocam_white_24dp));
PrimaryDrawerItem item5 = new PrimaryDrawerItem().withIdentifier(4).withName(R.string.drawer_item_theatres).withIcon(getResources().getDrawable(R.drawable.ic_theaters_white_24dp));
PrimaryDrawerItem item6 = new PrimaryDrawerItem().withIdentifier(5).withName(R.string.drawer_item_location).withIcon(getResources().getDrawable(R.drawable.ic_location_on_white_24dp));
SecondaryDrawerItem item7 = (SecondaryDrawerItem) new SecondaryDrawerItem().withIdentifier(6).withName(R.string.drawer_item_about_us).withIcon(FontAwesome.Icon.faw_info_circle);
SecondaryDrawerItem item8 = (SecondaryDrawerItem) new SecondaryDrawerItem().withIdentifier(7).withName(R.string.drawer_item_contact_us).withIcon(FontAwesome.Icon.faw_whatsapp);
SecondaryDrawerItem item9 = (SecondaryDrawerItem) new SecondaryDrawerItem().withIdentifier(8).withName(R.string.drawer_item_feedback).withIcon(FontAwesome.Icon.faw_commenting);
SecondaryDrawerItem item10 = (SecondaryDrawerItem) new SecondaryDrawerItem().withIdentifier(9).withName(R.string.drawer_item_settings).withIcon(FontAwesome.Icon.faw_wrench);
// Create the AccountHeader
AccountHeader headerResult = new AccountHeaderBuilder()
.withActivity(this)
.withHeaderBackground(R.color.colorMaterialDark)
.addProfiles(
new ProfileDrawerItem().withName(getResources().getString(R.string.app_name)).withEmail(getResources().getString(R.string.email_id)).withIcon(getResources().getDrawable(R.drawable.profile))
)
.withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
#Override
public boolean onProfileChanged(View view, IProfile profile, boolean currentProfile) {
//Any activity or Intent
}
})
.build();
//Create the drawer and remember the `Drawer` result object
Drawer result = new DrawerBuilder()
.withActivity(this)
.withAccountHeader(headerResult)
.withToolbar(toolbar)
.addDrawerItems(
item1, item2, item3, item4, item5, item6,
new SectionDrawerItem().withName("Extras"),
item7, item8, item9, item10
)
//Set onClick options for drawer item click
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
#Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
// do something with the clicked item :D
switch (position) {
//Home
case 1: {
break;
}
//Login
case 2: {
//Login Activity
break;
}
case 3: {
//Third activity
break;
}
case 4: {
break;
}
case 5: {
break;
}
//Location
case 6: {
break;
}
//About us
case 8: {
break;
}
//Contact Us
case 9: {
break;
}
//Feedback
case 10: {
break;
}
//Settings
case 11:{
break;
}
}
return true;
}
})
.build();
result.addStickyFooterItem(new PrimaryDrawerItem().withName("Visit us again")); //Adding footer to nav drawer
}
You will have to add the following lines to build.gradle file in android studio
compile('com.mikepenz:materialdrawer:5.3.3#aar') {
transitive = true
}
compile 'com.mikepenz:fontawesome-typeface:4.6.0.2#aar'
i have implemented Drawer menu in my application. i have 7 different fragments in my app. the MainActivity which holds all the fragments is drawer activity. i have overriden the onBackPressed() method in this activity. i have a fragment where i would like user to save the data he has changed. that is if user have changed something in one fragment and without saving it if user leaves fragment (By selecting another fragment from drawer menu or by pressing a back button) then i want to show the confirmation dialog to user on same fragment where the changes were made. as an example.. if user have made some change in sixth fragment and he leaves the fragment (by selecting any other fragment or back button) i want to show user a confirmation dialog on sixth fragment itself. from where according to his input he can move the selected fragment or stay in the fragment where he made some changes.
Here is the snap of a code of my DrawerActivity.
#Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
if (mBackPressed + TIME_INTERVAL > System.currentTimeMillis())
{
super.onBackPressed();
AppConstants.SELECTED_NAVIGATION_TAB=0;
return;
}
else { Toast.makeText(getBaseContext(), "Tap back button in order to exit", Toast.LENGTH_SHORT).show(); }
mBackPressed = System.currentTimeMillis();
}
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
Fragment fragment = null;
switch (id) {
case R.id.nav_home:
AppConstants.SELECTED_NAVIGATION_TAB = 0;
fragment = new HomeFragment();
title = getResources().getString(R.string.home);
break;
case R.id.nav_progress:
AppConstants.SELECTED_NAVIGATION_TAB = 1;
fragment = new ProgressFragment();
title = getResources().getString(R.string.progress);
break;
case R.id.nav_workouts:
AppConstants.SELECTED_NAVIGATION_TAB = 2;
/* if (Utilities.getInstance().getPreferences(DrawerActivity.this, AppConstants.PREF_KEY_USER_INFO,
AppConstants.PREF_KEY_SUBSCRIPTION_STATUS, "").equals("1")){
fragment = new FragmentPremiumClasses();
title = getResources().getString(R.string.premium);
}else {*/
fragment = new WorkoutsFragment();
title = getResources().getString(R.string.workouts);
// }
break;
case R.id.nav_subscription:
AppConstants.SELECTED_NAVIGATION_TAB = 3;
if (!Utilities.getInstance().getPreferences(DrawerActivity.this, AppConstants.PREF_KEY_USER_INFO,
AppConstants.PREF_KEY_SUBSCRIPTION_STATUS, "").equals("1")){
fragment = new SubscriptionFragment();
title = getResources().getString(R.string.subscription);
} else {
fragment = new SelectSocietyFragment();
title = getResources().getString(R.string.newsubscription);
}
break;
case R.id.nav_aboutUs:
AppConstants.SELECTED_NAVIGATION_TAB = 4;
fragment = new AboutUsFragment();
title = getResources().getString(R.string.aboutus);
break;
case R.id.nav_settings:
AppConstants.SELECTED_NAVIGATION_TAB = 5;
fragment = new SettingsFragment();
title = getResources().getString(R.string.settings);
break;
case R.id.nav_logout:
drawer.closeDrawer(GravityCompat.START);
isLogoutClicked = true;
SharedPreferences.Editor editor = getSharedPreferences(AppConstants.PREF_KEY_USER_INFO,
Context.MODE_PRIVATE).edit();
editor.clear();
editor.commit();
AppConstants.SELECTED_NAVIGATION_TAB = 0;
Utilities.getInstance().finishAndStartIntent(DrawerActivity.this, MainActivity.class, 0, 0);
break;
}
if (!isLogoutClicked) {
getSupportActionBar().setTitle(title);
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame, fragment);
fragmentTransaction.commit();
drawer.closeDrawer(GravityCompat.START);
}
return true;
}
Hope i have made the question clear, how to achieve this functionality? any help would be much appreciated :)
thanks :)
Save data in onPause() of fragment.