-2

I want to save a value from my LoginActivity and Provide that to my MainActivity, but currently Log.d does not Show anything. I tried using shared preferences.

My LoginActivity:

// I want to save email to mainactivity
SharedPreferences webrtcOptions = getSharedPreferences("webrtcOptions", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = webrtcOptions.edit();
editor.putString("email", email);
editor.commit();

And in my MainActivity I try to call it like this:

    SharedPreferences webrtcOptions = getSharedPreferences("webrtcOptions", 0);
    String currentUserMail = webrtcOptions.getString("email", "test");
    Log.d("HERE", currentUserMail);
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Jaan
  • 251
  • 1
  • 6
  • 16
  • What do you mean by "Log.d does not Show anything"? Does it not show the value you expect, or is there no log statement at all? If there is no log statement at all, then you need to provide more details about where you have put this code. – EJK Mar 12 '18 at 16:40
  • Possible duplicate of [How to use SharedPreferences in Android to store, fetch and edit values](https://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values) – Thomas Mary Mar 12 '18 at 16:44

2 Answers2

1

Try putExtra to send values to another Activities in your LoginActivity:

Intent i = new Intent(LoginActivity.this, MainActivity.class);   
    String strName = null;
    i.putExtra("email", email);

In your MainActivity you get the value like this:

String newString;
if (savedInstanceState == null) {
    Bundle extras = getIntent().getExtras();
    if(extras == null) {
        newString= null;
    } else {
        newString= extras.getString("email");
    }
} else {
    newString= (String) savedInstanceState.getSerializable("email");
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
0

Log.d should at least show your string "HERE" if you can not see that it may have one of following reasons

  1. Your Log.d code is not executed, make sure it is executed
  2. Or your android studios log window has bad configuration then do the following:

If you do not see logs from your app check your Android studio's Log window then make sure you have selected

  • correct emulator or device
  • correct app
  • correct filter
  • make search field empty
ygngy
  • 3,630
  • 2
  • 18
  • 29