title: Android - Using a variable from another class?
body: <p>I'm making an Android application wherein a student logs in with his registration ID then can access services like view his grades, class schedule, etc. without entering his registration ID twice.</p>
  
  <p>My question here is, how can I get the registration ID he first entered during login and use it to access other activities so that he would not have to enter again?</p>
  
  <p>Here's is the code for the login activity:</p>
  
  <pre><code>    package com.example.kreshiathea.myfirstapp;
  
      import java.util.ArrayList;
      import java.util.List;
  
      import org.apache.http.HttpResponse;
      import org.apache.http.NameValuePair;
      import org.apache.http.client.HttpClient;
      import org.apache.http.client.ResponseHandler;
      import org.apache.http.client.entity.UrlEncodedFormEntity;
      import org.apache.http.client.methods.HttpPost;
      import org.apache.http.impl.client.BasicResponseHandler;
      import org.apache.http.impl.client.DefaultHttpClient;
      import org.apache.http.message.BasicNameValuePair;
  
      import android.app.Activity;
      import android.app.AlertDialog;
      import android.app.ProgressDialog;
      import android.content.DialogInterface;
      import android.content.Intent;
      import android.os.Bundle;
      import android.view.View;
      import android.view.View.OnClickListener;
      import android.widget.Button;
      import android.widget.EditText;
      import android.widget.TextView;
      import android.widget.Toast;
  
      public class MainActivity2 extends Activity {
          Button b;
          EditText et,pass;
          TextView tv;
          HttpPost httppost;
  StringBuffer buffer;
  HttpResponse response;
  HttpClient httpclient;
  List&lt;NameValuePair&gt; nameValuePairs;
  ProgressDialog dialog = null;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main_activity2);
  
      b = (Button)findViewById(R.id.loginnext);
      et = (EditText)findViewById(R.id.rfid);
      tv = (TextView)findViewById(R.id.tv);
  
      String rfid = et.getText().toString().trim();
  
      Intent in = new Intent(getApplicationContext(), MainActivity3Activity.class);
      in.putExtra("rfid", rfid);
      startActivity(in);
  
  
      b.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
              dialog = ProgressDialog.show( MainActivity2.this, "",
                      "Validating user...", true);
              new Thread(new Runnable() {
                  public void run() {
                      login();
                  }
              }).start();
          }
      });
  }
  
  void login(){
      try{
  
          httpclient=new DefaultHttpClient();
          httppost= new HttpPost("http://usamobileapp.pe.hu/webservice/check.php"); // make sure the url is correct.
  
          nameValuePairs = new ArrayList&lt;NameValuePair&gt;(2);
  
          nameValuePairs.add(new BasicNameValuePair("username",et.getText().toString().trim()));  // $Edittext_value = $_POST['Edittext_value'];
          httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  
          response=httpclient.execute(httppost);
  
          ResponseHandler&lt;String&gt; responseHandler = new BasicResponseHandler();
          final String response = httpclient.execute(httppost, responseHandler);
          System.out.println("Response : " + response);
          runOnUiThread(new Runnable() {
              public void run() {
                  tv.setText("Response from PHP : " + response);
                  dialog.dismiss();
              }
          });
  
  
  
          if(response.equalsIgnoreCase("User Found")){
              runOnUiThread(new Runnable() {
                  public void run() {
                      Toast.makeText( MainActivity2.this,"Login Success", Toast.LENGTH_SHORT).show();
                  }
              });
  
              startActivity(new Intent( MainActivity2.this, MainActivity3Activity.class));
          }else{
              showAlert();
          }
  
      }catch(Exception e){
          dialog.dismiss();
          System.out.println("Exception : " + e.getMessage());
      }
  }
  public void showAlert(){
      MainActivity2.this.runOnUiThread(new Runnable() {
          public void run() {
              AlertDialog.Builder builder = new AlertDialog.Builder( MainActivity2.this);
              builder.setTitle("Login Error.");
              builder.setMessage("User not Found.")
                      .setCancelable(false)
                      .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int id) {
                          }
                      });
              AlertDialog alert = builder.create();
              alert.show();
          }
      });
  }
      }
  </code></pre>
  
  <p>I tried to uses an instance so I could import variable to another class. But I'm sure where to put it exactly so I placed it here:</p>
  
  <pre><code> @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main_activity2);
  
      b = (Button)findViewById(R.id.loginnext);
      et = (EditText)findViewById(R.id.rfid);
      tv = (TextView)findViewById(R.id.tv);
  
      String rfid = et.getText().toString().trim();
  
      Intent in = new Intent(getApplicationContext(), MainActivity3Activity.class);
      in.putExtra("rfid", rfid);
      startActivity(in);
  </code></pre>
  
  <p>This is the class where I want to import the variable <code>MainActivity3Activity</code></p>
  
  <p>Here's the code:</p>
  
  <pre><code>package com.example.kreshiathea.myfirstapp;
  
  import android.app.Activity;
  import android.content.Intent;
  import android.support.v7.app.ActionBarActivity;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.UnsupportedEncodingException;
  import java.util.ArrayList;
  import java.util.List;
  
  import org.apache.http.HttpResponse;
  import org.apache.http.NameValuePair;
  import org.apache.http.client.ClientProtocolException;
  import org.apache.http.client.HttpClient;
  import org.apache.http.client.entity.UrlEncodedFormEntity;
  import org.apache.http.client.methods.HttpGet;
  import org.apache.http.client.methods.HttpPost;
  import org.apache.http.impl.client.DefaultHttpClient;
  import org.apache.http.message.BasicNameValuePair;
  
  import android.widget.EditText;
  import android.widget.TextView;
  
  
  public class MainActivity3Activity extends Activity {
  
  Intent in = getIntent();
  String rfid = in.getStringExtra("rfid");
  
  
  HttpClient httpclient;
  HttpGet request;
  HttpResponse response;
  HttpPost httppost;
  List&lt;NameValuePair&gt; nameValuePairs;
  
  
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main_activity3);
  
      httpclient = new DefaultHttpClient();
      httppost = new HttpPost("http://usamobileapp.pe.hu/webservice/student_info.php");
  
  
  
      try {
          nameValuePairs = new ArrayList&lt;NameValuePair&gt;(2);
  
          nameValuePairs.add(new BasicNameValuePair("username", rfid));
          httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  
          response = httpclient.execute(httppost);
      } catch (ClientProtocolException e) {
          e.printStackTrace();
      } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
  
      TextView result = (TextView) findViewById(R.id.tvResult);
  
      try {
          request = new HttpGet("http://usamobileapp.pe.hu/webservice/student_info.php");
          response = httpclient.execute(request);
      } catch (Exception e) {
          e.printStackTrace();
      }
  
      try {
          BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
  
          String line = "";
             while ((line = rd.readLine()) != null) {
                 result.append(line);
             }
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>I place the receiving intent here:</p>
  
  <pre><code>public class MainActivity3Activity extends Activity {
  
  Intent in = getIntent();
  String rfid = in.getStringExtra("rfid");
  
  
  HttpClient httpclient;
  HttpGet request;
  HttpResponse response;
  HttpPost httppost;
  List&lt;NameValuePair&gt; nameValuePairs;
  </code></pre>
  
  <p>The <code>MainActivity3Activity</code> class also displays the student information (using the previously entered registration ID).</p>
  
  <p>I'm not sure if I correctly used and placed the intents. So please I'm looking for any help.</p>