Skip to content Skip to sidebar Skip to footer

How To Bypass Intent Extras In Android?

I want to know if there is a way to pass all the extras to the new intent. Is it possible? And is it a good idea? The reason is that I want to save the login state as a json string

Solution 1:

please consider Shared Preference :

// DeclarationpublicstaticStringKEY = "SESSION";

   publicstaticvoidsaveUserName(String username, Context context) {
        Editor editor = context
                .getSharedPreferences(KEY, Activity.MODE_PRIVATE).edit();
        editor.putString("username", username);
        editor.commit();
    }

    publicstaticStringgetUserName(Context context) {
        SharedPreferences savedSession = context.getSharedPreferences(KEY,
                Activity.MODE_PRIVATE);
        return savedSession.getString("username", "");
    }

You can store the login credentials to preference and retrieve them as well in any of your activity.

SaveUsername("Your text to save", Your_activity.this);

And to retrieve the value

StringmUserName= getUserName(YourActivity.this);

It is recommended to have all your preference methods in your utils class to keep the code organized.

You can read more here

Solution 2:

yes you can use Shared Preference to save the Login session of user here is handy example of managing session using shared preference http://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/ I used it earlier and suggest you to have a look at it.

Solution 3:

Any of the two may help you.

  1. Saving it in the sharedPreference file.
  2. If you want them to be accessable throught the application you can use the applicationClass, like a global singleton.

Post a Comment for "How To Bypass Intent Extras In Android?"