Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a session object in an application

In my app i want to create a session for login and log out.

I dont have any idea how to work with session. Anybody help me by giving some sample example.

like image 400
Jyosna Avatar asked Feb 15 '26 05:02

Jyosna


1 Answers

I think the session object should be a static object declared and initialized when your application starts running. I have met this problem and decided to put my session object in a utils class which contains mathods used by every activity in my app.

Here is a short example:

  1. create a class for utils which will contain session object, Session is the class by which you implement your session object. It can contain, for example, userId, userName, etc.:

    public class Utils {  
        public static Session mySessionObject = null;
    }
    
  2. When login button is pushed initialize your session object:

    login.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Utils.mySessionObject = new Session();
           //some extra initalization, for example setting userId
        }
    });
    

On logout you can destroy your session object.

Here is a link telling more about sessions.

like image 145
superM Avatar answered Feb 16 '26 19:02

superM