0

I need to pass the username & token that return from json response(logcat successful) from my LoginActivity so that I can set them as parameters for a new http request.

How can I do it?

LoginActivity

CarListFragment

By the way, If possible can you also teach me how can I display all the cars using foreach loop or something!

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
vPhong
  • 41
  • 10

1 Answers1

1

You can pass your parameter as bundle data to fragment.

CarFragment carfragment= new CarFragment();
 Bundle bundle=new Bundle();
    bundle.putString("username",username);
    bundle.putString("token",token);
   carfragment.setArguments(bundle);

getSupportFragmentManager().beginTransaction().replace(R.id.your_container,carFragment).commit();

you can retrieve this data from carfragment onCreate() method

  Bundle bundle= getArguments();
    String username= bundle.getString("username");
    String token = bundle.getString("token");

For each can be coded as

for(Car car : carList){
// do your stuff using "car" variable
}

or

you can use SharedPreference to store username and token

in your login callback result ,save username and token

SharedPreferences preferences=getSharedPreferences("myPref",MODE_PRIVATE);
    SharedPreferences.Editor editor= preferences.edit();
    editor.putString("username",username);
    editor.putString("token",token);
    editor.apply();

in your carFragment use this code to retrieve data

 SharedPreferences preferences=getActivity().getSharedPreferences("myPref", Context.MODE_PRIVATE);
    String username=preferences.getString("username","");
    String token = preferences.getString("token","");

Note: Storing sensitive data in preference is risky.. So try to encrypt your token. and decrypt when ever you need..

Vinayak B
  • 4,430
  • 4
  • 28
  • 58
  • Vinayak, Im not sure what container in replace() method, What do I have to put there? – vPhong Aug 07 '17 at 07:27
  • Put your framelayout id in that place. Its in your xml file – Vinayak B Aug 07 '17 at 07:46
  • I understand. I need to try! – vPhong Aug 07 '17 at 07:55
  • Vinayak. I got this error. java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference at com.vnetgps.topgps.BottomNavigationFragments.CarListFragment.onCreateView(CarListFragment.java:47) – vPhong Aug 07 '17 at 09:03
  • are you calling your fragment from Login activity or main activity? – Vinayak B Aug 07 '17 at 09:07
  • I send the username & password from LoginActivity to MainActivity first (Test this ok). Then from MainActivity to CarListFragment, I follow your instructions. – vPhong Aug 07 '17 at 09:22
  • I am reading this as well. https://stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android – vPhong Aug 07 '17 at 09:23
  • Can you double check the key("username" and "token") used in main activity and car fragment are same. – Vinayak B Aug 07 '17 at 09:30
  • Sure It is the same, Vinayak. I found out that It works/shows only for the first time, if I click the button again. The application crashed and give me that NullPointerException. – vPhong Aug 07 '17 at 09:37
  • If you want to use your token multiple times the you can save your data in preferences. I edited my ans. please check it – Vinayak B Aug 07 '17 at 10:00
  • Okay, I will try and let you know later. Thanks alot! – vPhong Aug 07 '17 at 10:14