How to save which Navigation Drawe item I selected

1
Good morning, guys, I'm developing an app and I put a navigation drawer in it pretty basic, and I'd like to know how to "save" which item is selected. And so whenever the app opens, it will open in the fragment that I put in that menu item (navigation drawer)

Ex for better understanding:

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

Thank you in advance

    
asked by anonymous 28.05.2016 / 05:37

3 answers

0

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

After the user logs in to my app I save the user and the password and the next time he logs in I check if the file exists and I read the data and log in automatically, if all is correct, the user is directed to the main screen, otherwise it 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 to this file and check which item was selected. And when the user makes a new selection, you change the file.

    
28.05.2016 / 06:20
0

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

    
02.02.2018 / 14:51
0

One option is to use Android's SharedPreferences . It is a mechanism for storing key-values . The documentation has some examples of how to use this class.

When the user clicks on an item in the drawer you could save in a key what the name of the sign he has 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 retrieve this sign and do something with this 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");
    
02.02.2018 / 16:21