Skip to content Skip to sidebar Skip to footer

Save Intent In Shared Preference

I have an application from which i can launch other apps installed on my phone, with a long click i get the app picker, in result i receive an intent data, how can i save it so the

Solution 1:

Ok i found a way I save the intent like this

SharedPreferences settings = getSharedPreferences(PREFERENCES, 0);
                SharedPreferences.Editor editor = settings.edit();
                String uriString = data.toUri(requestCode); 
                editor.putString("Contacts_app", uriString);
                editor.commit();

Then i retrieve it like this

SharedPreferences settings = getSharedPreferences(PREFERENCES, 0);
    String contactsApp = settings.getString("Contacts_app", null);
    try {
        telApp = Intent.parseUri(contactsApp, 0);
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Solution 2:

You could serialize the object to a string, and save the resulting string in the preferences. An easy way would be to serialize it in json format, using Google Gson for example.


Post a Comment for "Save Intent In Shared Preference"