How to store which Navigation Drawe item I selected

Asked

Viewed 48 times

1

Good morning guys, I’m developing an app and put a navigation Drawer in it very basic, and I would like to know how to "save" which item is selected. And so whenever the app opens, it will open in the Fragment I put in this menu item (navigation Drawer)

Ex for better understanding:

My app is horoscope and my menu contains all the signs, if I selected "pound" every time my app opens, it will open in the pound Fragment.

Thank you in advance

  • try to save this information in a basic database and change the setcontentview by placing the content equal to the database variable

3 answers

0

Put a variable that when you click on such an item it takes such value, eg: clicked on item 1 the variable arrow 1, then you make the app save this data in localstorage, when it opens again load the data and then do what you want.

0

I do something very little like what you are looking for, in my case is automatic login.

After the user login in my app I save the user and the password and the next time he enters I check if the file exists and read the data and do the automatic login, if everything goes well, the user is directed to the main screen, otherwise goes to the login screen.

In your case, you can save a configuration file containing the selected item, and when the app starts you go into this file and verify which item was selected. And when the user makes a new selection, you change the file.

  • I will study this, because I’m starting in android development and do not know many things, if you can give a tip of where to start thank

0

One option is to use Sharedpreferences android. It is a mechanism for storing key-values. In the documentation you have some examples of how to use this class.

When the user clicks on an item in the drawer you could store in a key which sign name he accessed:

Context context = getActivity();
SharedPreferences sharedPref = context.getSharedPreferences("nome_do_arquivo", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("ultimo_signo", "nome_do_signo_clicado");
editor.commit();

And when he opens the app again, you can recover this sign and do something with that information:

Context context = getActivity();
SharedPreferences sharedPref = context.getSharedPreferences("nome_do_arquivo", Context.MODE_PRIVATE);
String ultimoSigno = sharedPref.getString("ultimo_signo", "valor_padrao_caso_nao_exista_um_signo_ainda");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.