title: Accessing username from all classes: Android
body: <hr>
  
  <p>Background Information:</p>
  
  <p>I am developing a small application. The way it works right now is the moment the app is launched, the user is prompted with the main activity. Then from there, the user clicks on the login button which prompts the login activity. Now in my login activity, I have set an Intent that gets the user's username and then stores it in the Shared Preferences. If the login is successful then the user goes to the mainloggedinpage where the user sees: [Welcome back {username}]</p>
  
  <hr>
  
  <p>The problem is the following:</p>
  
  <p>If the user is accessing the mainloggedinpage via loginactivity then you can easily see his username which is exactly what I want. But the thing is that on the main page, I have other buttons which goes to other activities. The moment the user click on any of those activities and then returns to the mainloggedin page, then all you see is: [Welcome Back {}]. His username is not displayed anymore.</p>
  
  <hr>
  
  <p>My question: </p>
  
  <p>Can anyone suggest a quick fix that will allow me to display the username so even when the user goes to another activity from the mainloggedinpage and then comes back to the mainloggedinpage, the user will still see: [Welcome Back {username}]?</p>
  
  <hr>
  
  <p>My Code for Login Activity [This is where I am getting the username]:</p>
  
  <pre><code>    @Override
      protected String doInBackground(String... args) {
          // TODO Auto-generated method stub
           // Check for success tag
          int success;
          String username = user.getText().toString();
          String password = pass.getText().toString();
          try {
              // Building Parameters
              List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;();
              params.add(new BasicNameValuePair("username", username));
              params.add(new BasicNameValuePair("password", password));
  
              Log.d("request!", "starting");
              // getting product details by making HTTP request
              JSONObject json = jsonParser.makeHttpRequest(
                     LOGIN_URL, "POST", params);
  
              // check your log for json response
              Log.d("Login attempt", json.toString());     
  
              // json success tag
              success = json.getInt(TAG_SUCCESS);
              if (success == 1) {
  
                  Log.d("Login Successful!", json.toString());
                  // save user data
                  SharedPreferences sp = PreferenceManager
                          .getDefaultSharedPreferences(Login.this);
                  Editor edit = sp.edit();
                  edit.putString("username", username);
                  edit.commit();
  
                  Intent i = new Intent(Login.this, mainpage.class);
                  i.putExtra("Welcome back, username", username);
                  finish();
                  startActivity(i);
                  return json.getString(TAG_MESSAGE);
              }else{
                  Log.d("Login Failure!", json.getString(TAG_MESSAGE));
                  return json.getString(TAG_MESSAGE);
  
              }
          } catch (JSONException e) {
              e.printStackTrace();
          }
  
          return null;
  
      }
  </code></pre>
  
  <hr>
  
  <p>My Code for mainloggedinActivity [This is where the username is displayed]:</p>
  
  <pre><code>    protected void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
      setContentView(R.layout.mainpage);
  
      msuggestions = (Button) findViewById(R.id.btn_suggestions);
      msuggestions.setOnClickListener(this);
  
      mprayertimes = (Button)findViewById(R.id.btn_activityone);
      mprayertimes.setOnClickListener(this);
  
      mqibladirection =(Button)findViewById(R.id.btn_activitytwo);
      mqibladirection.setOnClickListener(this);
  
      mnewsboard = (Button) findViewById(R.id.newsboard);
      mnewsboard.setOnClickListener(this);
  
      mhadiths = (Button) findViewById(R.id.btn_activitythree);
      mhadiths.setOnClickListener(this);
  
      mchat = (Button) findViewById(R.id.btn_discussion);
      mchat.setOnClickListener(this);
  
      mallahnames = (Button) findViewById(R.id.btn_activityfour);
      mallahnames.setOnClickListener(this);
  
      // Get Username Start
      TextView txt_loggedName = (TextView) findViewById(R.id.txt_loggedName);
  
      Intent intent = getIntent();
      String username  = intent.getStringExtra("Welcome back, username");
      txt_loggedName.setText(username);
      // Get Username End
  
  
      buttonLogout = (Button) findViewById(R.id.logout);
      buttonLogout.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
           mPreferences = getSharedPreferences("CurrentUser", MODE_PRIVATE); 
           SharedPreferences.Editor editor=mPreferences.edit();
           editor.remove("username");
           editor.remove("password");
           editor.commit();
           Message myMessage=new Message();
                  myMessage.obj="NOTSUCCESS";
                  handler.sendMessage(myMessage); 
           finish();
          }
    });
  </code></pre>
  
  <hr>
  
  <p>Can anyone help me out so then when a user goes to the mainpage and click on one of the button and then comes back to the mainpage, it still displays his username? :)</p>