androidandroid-activityserverlistviewitemrole

How to open Android activities based on role of user?


I am creating an online based Android app in which there is a listview, which shows notifications from different types of users like 'student', 'teacher'. When I click a list view item, it should check the role of the user, whether the user is registered as 'teacher' or a 'student' and it should open the activity screen based on their role. I'll use intents to open different activities. How do I check the role of the user from server?


Solution

  • Well, I would like to provide my own answer. I actually used Shared Preferences. Its much simple and could globally use the values we put in it. Below is the code:

    1. Create a separate class and name it as you wish(I prefer SessionManager here)

    public class SessionManager {
    
       // Shared Preferences
       SharedPreferences sharedPrefer;
    
       // Editor for Shared preferences
       SharedPreferences.Editor editor;
    
       // Context
       Context context;
    
       // Shared Pref mode
       int PRIVATE_MODE = 0;
    
       // Shared Pref file name
       private static final String PREF_NAME = "MySession";
    
       // SHARED PREF KEYS FOR ALL DATA
    
       // User's UserId
       public static final String KEY_USERID = "userId";
    
       // User's categoryId
       public static final String KEY_CATID = "catId";
    
       // User's categoryType[Teacher, Student, etc.,]
       public static final String KEY_CATTYPE = "categoryType";
    
       // User's batchId[like class or level or batch]
       public static final String KEY_BATCHID = "batchId";
    
    
    
        // Constructor
        public SessionManager(Context context) {
           this.context = context;
           sharedPrefer = context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
           editor = sharedPrefer.edit();
        }
    
    /**
     * Call this method on/after login to store the details in session
     * */
    
       public void createLoginSession(String userId, String catId, String catTyp, String batchId) {        
    
           // Storing userId in pref
           editor.putString(KEY_USERID, userId);
    
           // Storing catId in pref
           editor.putString(KEY_CATID, catId);
    
           // Storing catType in pref
           editor.putString(KEY_CATTYPE, catTyp);
    
           // Storing catType in pref
           editor.putString(KEY_BATCHID, batchId);
    
           // commit changes
           editor.commit();
       }
    
    /**
     * Call this method anywhere in the project to Get the stored session data
     * */
       public HashMap<String, String> getUserDetails() {
    
           HashMap<String, String> user = new HashMap<String, String>();
           user.put("userId",sharedPrefer.getString(KEY_USERID, null));
           user.put("batchId",sharedPrefer.getString(KEY_BATCHID, null));      
           user.put("catId", sharedPrefer.getString(KEY_CATID, null));
           user.put("catType", sharedPrefer.getString(KEY_CATTYPE, null));
    
           return user;
       }
    }
    

    2. Calling the above methods on some other classes inside the project:

    Storing the data in session

    SessionManager session = new SessionManager(getApplicationContext());
    session.createLoginSession(userId, categoryId, categoryType, batchId);
    

    Retrieving the data from session

    SessionManager session = new SessionManager(getApplicationContext());
    HashMap<String, String> user = session.getUserDetails();
    String userId = user.get("userId").toString();
    String categoryId = user.get("catId").toString();
    String categoryType = user.get("catType").toString();
    String batchId= user.get("batchId").toString();