0

I think taht's a Context problem, but I could not figure it out.

I'm retrieving a user id from webview with JavascriptInterface Interface. I'm using WebAppInterface class to retrieve the value and save it in sharedprefs. I could test the returned value and it's ok. It is saved in the SharedPreferences when I pull it from inside WebAppInterfacebut when I try to retrieve it in another activity the value retrieved is default.

The webview activity:

public class Login extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        WebView myWebView = (WebView) findViewById(R.id.webview);
        myWebView.setWebViewClient(new WebViewClient());
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
        myWebView.loadUrl("mydomain.com/retrieveid"); //here I use the real url
    }
 ...
}

The WebAppInterfaceClass

public class WebAppInterface {
    Context mContext;
    SharedPreferences mPrefs;
    WebAppInterface(Context c) {
        mContext = c;
        mPrefs = c.getSharedPreferences("My_Prefs", 0);
    }

    @JavascriptInterface
    public void returnUserID(String uid) {
        Editor editor = mPrefs.edit();
        editor.putInt("uid", Integer.valueOf(uid));
        editor.commit();
        Integer uid2 = mPrefs.getInt("uid", 0);
        Log.i("uid after update", String.valueOf(uid2)); //this value is correct
        Intent intent = new Intent(mContext, MainActivity.class);
        mContext.startActivity(intent);
   }
}

Then in MainActivity.class I try to retrive this value with getSharedPreferences("My_Prefs", 0).getInt("uid",0); and it always return 0.

Ricardo Gonçalves
  • 4,344
  • 2
  • 20
  • 30
  • Do the 2 activities live in separate processes? See http://stackoverflow.com/questions/12125214/shared-preferences-between-two-processes-of-the-same-application – cybersam Sep 11 '15 at 17:15
  • I think that's the issue... I'll try it and update here. – Ricardo Gonçalves Sep 11 '15 at 17:18
  • See my updated answer. It looks like writing a ContentProvider or a Service is the right answer. – cybersam Sep 11 '15 at 17:57
  • Please check two things: 1. Using the **ApplicationContext** 2. Use the c.getSharedPreferences("My_Prefs", **Context.MODE_MULTI_PROCESS**); Try it. Hope it will help you fix it. – Bruce Lan Sep 11 '15 at 18:33

2 Answers2

1

[Updated]

Scenario 1 - The 2 activities are in separate apps

When calling getSharedPreferences(String name, int mode), the mode argument determines whether the stored values are private, or globally readable and/or writeable.

MODE_PRIVATE (whose value 0) is probably what you used in the app that originally stored the data. That means that no other apps have any access to that data.

You should be aware that the other modes have been deprecated since API Level 17, though, since they open up security holes.

You should consider having the first app implement either a ContentProvider or Service to provide the shared data.

Scenario 2 - The 2 activities are in the same app, but in different processes

Refer to this question and its accepted answer. Please note, though, that MODE_MULTI_PROCESS is deprecated in API level 23 as it does not work reliably in some versions of Android and does not attempt to reconcile concurrent modifications across processes.

You should consider implementing either a ContentProvider or Service to provide the shared data. For very simple data, a FileProvider (a ContentProvider for files) might be sufficient.

Community
  • 1
  • 1
cybersam
  • 63,203
  • 6
  • 53
  • 76
1
  1. Basically don't pass values between activities in SharedPreferences, that's what are Intent Extras are for.

The rough Idea is:

Intent intent = new Intent(mContext, MainActivity.class);
intent.putExtra("uid", uid);
mContext.startActivity(intent);

and then in MainActivity

getIntent().getIntExtra("uid", -1);

will return your value or -1

  1. It's a bit weird that you are triggering the MainActivity with an Intent from inside the app which makes me think you should actually have started the current activity with startActivityForResult: http://developer.android.com/training/basics/intents/result.html
Raanan
  • 4,777
  • 27
  • 47
  • I'm not actually calling mainactivity.. That's just a sample. I'm using some variantes to redirect the user to 3 diferentes activities. I was using intents, but I need this uid in all activities and next time the uawr launch the app... – Ricardo Gonçalves Sep 11 '15 at 18:14
  • If you are using single process then my suggestion then is to create a SharedPrefs singleton, so you'll only have one instance of SharedPreferences and therefore also Memory updates (apply) will be enough. – Raanan Sep 11 '15 at 18:44
  • 1
    I've passed the value as a Intent Extra to the other activity and saved it as a SharedPref there. Thanks for the insight. – Ricardo Gonçalves Oct 05 '15 at 20:06