views:

1362

answers:

3

dear friends,

i have a scenario of login page after logging in there will be sign out button on each activity.

on clicking signout i will be passing session id of signed in user to signout. can any one guide me how to keep session id available on all activities??

or any other solution to achieve this??

+2  A: 

The easiest way to do this would be to pass the session id to the signout activity in the intent you're using to start the activity:

Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("EXTRA_SESSION_ID", sessionId);
startActivity(intent)

The docs for Intents has more information (look at the section titled "Extras").

Erich Douglass
ok if i pass session id to signout acitivity on successful login and will it work on any activity page to signout or manually i will have to assign it value on each activity??? using above procedure??
UMMA
Yes, you'd have to make the session ID available to every activity where you want to allow the user to signout. Alternatively, you could store it in the Application object, but then you'd have to manage the state of the session (check if it's valid before using, etc).
Erich Douglass
+1  A: 

Hello, Try to do the following:

Create simple "helper" class(factory for your Intents), like this:

import android.content.Intent;
public class IntentManager {
public static final Intent createYourSpecialIntent(Intent src) {
  return new Intent("YourSpecialIntent").addCategory("YourSpecialCategory").putExtras(src);
}
}

This will be the factory for all your Intents. Everytime you need a new Intent, create static factory method in IntentManager. To create new Intent you should just say like that:

IntentHelper.createYourSpecialIntent(getIntent());

in your activity. when when you want to "save" some data in "session" just use following:

IntentHelper.createYourSpecialIntent(getIntent()).putExtra("YOUR_FIELD_NAME",fieldValueToSave);

and send this Intent. In target Activity your field will be available as:

getIntent().getStringExtra("YOUR_FIELD_NAME");

So now we can use Intent like same old session(like in servlets or jsp.). Hope this helps.

ponkin
+1  A: 

Passing Intent extras is a good approach as Erich noted.

The Application object is another way though, and it is sometimes easier when dealing with the same state across multiple activities (as opposed to having to get/put it everywhere), or objects more complex than primitives and Strings.

You can extend Application, and then set/get whatever you want there and access it from any Activity (in the same application) with getApplication().

Also keep in mind that other approaches you might see, like statics, can be problematic because they can lead to memory leaks. Application helps solve this too.

Charlie Collins