enter image description here

I have very little experience in developing on Android. Tell me how to make global mToken to install it in other places? ... in this example, where is the error

private MenuItem setupMenu(Menu menu) { // title list String[] titles = getResources().getStringArray(R.array.navigation_title_list); FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener( MainActivity.this, new OnSuccessListener<InstanceIdResult>() { @Override public void onSuccess(InstanceIdResult instanceIdResult) { String mToken = instanceIdResult.getToken(); } }); String[] urls = getResources().getStringArray(R.array.navigation_url_list+"?"+mToken ); // icon list TypedArray iconTypedArray = getResources().obtainTypedArray(R.array.navigation_icon_list); Integer[] icons = new Integer[iconTypedArray.length()]; for (int i = 0; i < iconTypedArray.length(); i++) { icons[i] = iconTypedArray.getResourceId(i, -1); } iconTypedArray.recycle(); // clear menu menu.clear(); // add menu items Menu parent = menu; MenuItem firstItem = null; for (int i = 0; i < titles.length; i++) { if (urls[i].equals("")) { // category parent = menu.addSubMenu(Menu.NONE, i, i, titles[i]); } else { // item MenuItem item = parent.add(Menu.NONE, i, i, titles[i]); if (icons[i] != -1) item.setIcon(icons[i]); if (firstItem == null) firstItem = item; } } return firstItem; } 
  • Not sure if I give the right advice, but in that case I used the class. Created a class something like GlobalVar and in it public variables. Seen from everywhere. - Stariy 6:43 pm
  • Isn't it easier to save in the SharedPreference? - danilshik 6:51 pm
  • The class is easier, I think. And faster. SharedPreference is very redundant for such a task, and they, if memory does not change, store values ​​in the file, this can affect the speed. And to make a class and add static variables to it, if necessary, you can’t think of something easier. And then from anywhere in the program you write GolbalVar.variable1 = 347 - what could be simpler? - Stariy
  • Comments are not intended for extended discussion; conversation moved to chat . - YurySPb

1 answer 1

Announce the mToken variable near the titles variable, then it will be visible anywhere in this function.