0

I am letting my user login to Facebook, Twitter and G+ on my app. After they log in, I want to know that they are logged in on every activity in my app. I am looking to find a way to find this out on each activity. I know you can pas variables from activity to activity using Intent like this:

            Intent intent = new Intent(MainActivity.this,
                    SecondActivity.class);
            intent.putExtra("loginMethod", "facebook");
            startActivity(intent);

but this is becoming a hassle to do with 10+ activities. Is there any easier way?

Harry
  • 772
  • 10
  • 32

4 Answers4

0

You can use SharedPreferences.

Here is a good tutorial:

http://examples.javacodegeeks.com/android/core/content/android-sharedpreferences-example/

Jonas452
  • 420
  • 6
  • 19
0

what I recomendo you is to implement shared preferences... going by this way

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor editor = preferences.edit();

editor.putstring("loginMethod","Facebook");
editor.commit;

and whenever you want to check the loginMethod value just call the preference value like this...

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());


String loginMethodValue = preferences.getString("loginMethod", "");

if you need more assistant I'll be more than happy to help you

geekCode
  • 336
  • 2
  • 11
0

You have many ways for doing so.

  1. Store it in a singleton
  2. Use SharedPreferences
  3. You can extend the Application context

More info here

Community
  • 1
  • 1
Lior Ohana
  • 3,467
  • 4
  • 34
  • 49
0

I suggest you to use a Singleton class. The mimimum contents of a singleton are:

  1. A static private field of the class type.
  2. Other private fields you need ("business" fields).
  3. A private constructor.
  4. A public static method that returns the only class instance.
  5. The business methods you need.

I usually call it Store, since it plays the role of a common store for the whole application. For example:

public class Store {
    private static Store me;
    private Facebook fb;
    private Twitter twitter;

    private Store() {
         this.fb=new Facebook();
         this.twitter=new Twitter();
    }

    public static Store get() {
         if (me==null) 
             me=new Store();
         return me;
    }

    public void postInFaceBook(FBPost post) {
        this.fb.post(post);
    }

      ...
}

Sometimes you need a context to be shared among activities. In this case, I suggest to pass the application context to the get method:

public class Store {
    private static Store me;
    private Facebook fb;
    private Twitter twitter;
    private Context ctx;

    private Store(Context ctx) {
         this.ctx=ctx;
         this.fb=new Facebook();
         this.twitter=new Twitter();
    }

    public static Store get(Context ctx) {
         if (me==null) 
             me=new Store(ctx);
         return me;
    }

    public void postInFaceBook(FBPost post) {
        this.fb.post(this.ctx, post);
    }

      ...
}

The use of the Store is simply:

public void m(FBPost p) {
    Store.get().postInFacebook(p);
}

The first time the store is used, the only instance (which will be shared by the whole application) will be created; the second and next times, the get method will return the previous existing instance.

  • Thanks for the post, is there an advantage to using this over SharedPreferences as the others suggested? Just curious is all – Harry Jul 31 '14 at 20:09
  • For my is easier since I keep the complete control and do as I want. I've never found any drawback to the use of "singletons". – Macario Polo Usaola Jul 31 '14 at 20:15
  • See also the use of a singleton in: [http://stackoverflow.com/questions/7742776/cant-register-receiver-dynamically/25003574#25003574](http://stackoverflow.com/questions/7742776/cant-register-receiver-dynamically/25003574#25003574) – Macario Polo Usaola Jul 31 '14 at 20:18