0

I have been searching on the Internet about how to create a Session in an Android application. I found this and it certainly helped me in a part of my project.

At present, I have a similar situation where in I have a Login application that asks the user to enter the username and password. On click of the submit button I create and initialize my Session object.

The problem I am facing now is the fact that I want to know whether I can use this object in various activities apart from my Login application activity. I also want to know that if the Session object contain user credentials , can it be send via a web service and stored in a remote database?

Community
  • 1
  • 1
Parth Doshi
  • 4,200
  • 15
  • 79
  • 129

3 Answers3

1

Create a class that extends Application (don't forget to update your manifest file) and add to it the required fields (username, password and so on). This class will be available to all your activities. You can find a detailed explanation here.

Later edit:

Let's say you have a Session class with username as a field.

class Session {
    public String username = "";
}

Next, extend the Application:

class App extends Application {
    Session session = new Session();

    public String getUsername() {
        return session.username;
    }

    public void setUsername(String username) {
        session.username = username;
    }
}

Now, when you want to access the username, you can do it from any activity:

App app = (App) getApplication();
String username = app.getUsername();
if (username.equals("")) {
    // the user is not logged in, do something
}
Community
  • 1
  • 1
Gabriel Negut
  • 13,860
  • 4
  • 38
  • 45
  • Thanks for the reply !! I have a few doubts. Please correct me if I am wrong. I have a class at present in my project , that has 2 EditText boxes for taking in the user name and password. I have created a Session Object on submit of this data. As u said, I need to create another class that extends Application. My doubt is that i n my new class do I need to create session object again ? How would I use the session object I created earlier in my new class? I saw the link but I couldn't understand it well..Can u explain it to me in simpler terms? – Parth Doshi Nov 09 '11 at 11:22
1

Or else try using sharedpreference if requirements are basic. http://developer.android.com/guide/topics/data/data-storage.html voteup and accept answer if you find it useful

drooooooid
  • 1,574
  • 1
  • 11
  • 17
  • Yes, I have heard of SharedPreferences before, but my requirements are not basic. I need to manage, create and destroy sessions in my project not just storage of data..Thanks anyways !! :D – Parth Doshi Nov 09 '11 at 11:30
  • fine.thought it may be helpful.thats all:) – drooooooid Nov 09 '11 at 12:08
0

Sorry, i maybe very late here but i like to introduce two points :

1 - There are various ways of storing objects to be available everywhere in the app. You can use static objects, Singleton design pattern, Serialization...etc. Every choice has its conditions (mostly related to object size)

2 - The accepted answer above introduced by @Gabriel Negut is not recommended due to the manner that Android manages its applications lifecycles. A NullPointerException can be thrown when the application is restored with a new Application object. Philippe Breault has a nice article about that.

GB11
  • 175
  • 1
  • 18