Log in but it does not show the next view

1

I have a login problem correctly in LoginActivity does not show the next view that is MainActivity.

  

LoginActivity.java

public class LoginActivity extends AppCompatActivity {

// Creo los elementos
EditText etMatricula, etPass;
public Button btnIngresar;
public TextView txtView;
JSONArray ja;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    // Inicializo los elementos
    etMatricula = (EditText)findViewById(R.id.input_matricula);
    etPass = (EditText)findViewById(R.id.input_password);
    btnIngresar = (Button) findViewById(R.id.btn_ingresar);
    txtView = (TextView) findViewById(R.id.link_registrar);

    btnIngresar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //pongo (ip/carpeta_proyecto/archivo) y veo que el usuario sea el que esta en el EditText matricula
            ConsultaPass("http://xxx.xxx.x.xx/ride_app/consulta.php?user="+etMatricula.getText().toString());
        }
    });

    txtView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent ride = new Intent(LoginActivity.this, RegistroActivity.class);
            startActivity(ride);
        }
    });

}//onCreate

private void ConsultaPass(String URL) {

    Log.i("url",""+URL);

    RequestQueue queue = Volley.newRequestQueue(this);
    StringRequest stringRequest =  new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

            try {
                //en el JA guarda la espuesta que le manda el WEB SERVICE
                ja = new JSONArray(response);
                //pone en contra la contraseña que se encuentra en el JA
                String contra = ja.getString(2);
                //si contra es igual a lo que se encuentra en el EditText pasa
                if(contra.equals(etPass.getText().toString())){
                    //muestro un toast de bienvenido
                    Toast.makeText(getApplicationContext(),"Bienvenido", Toast.LENGTH_SHORT).show();
                    //creo el intent
                    Intent main = new Intent(LoginActivity.this, MainActivity.class);
                    //lanzo el intent
                    startActivity(main);

                }else{ //de lo contrario
                    //muestro mensaje de error
                    Toast.makeText(getApplicationContext(),"verifique su contraseña", Toast.LENGTH_SHORT).show();

                }

            } catch (JSONException e) {
                e.printStackTrace();
                //si el servidor no devuelve nada mostrar mensaje de error
                Toast.makeText(getApplicationContext(),"El usuario no existe en la base de datos", Toast.LENGTH_LONG).show();
            }


        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });

    queue.add(stringRequest);



}

}

    
asked by Javier fr 15.05.2017 в 22:54
source

4 answers

0
try {
  jsonObject = new JSONObject(JSON_STRING);
  JSONArray result = jsonObject.getJSONArray(response);
  if(result.length()==0){
    Toast.makeText(access.this,"Acceso Incorrecto",Toast.LENGTH_LONG).show();
      Limpiar();
  }else{
    JSONObject jo = result.getJSONObject(0);
    String id = jo.getString("clave");
    String rol = jo.getString("rol");
    switch (rol){
      case "Root":
      Toast.makeText(access.this,"Correcto",Toast.LENGTH_LONG).show();
          Limpiar();
      break;

      default:
        Toast.makeText(access.this,"Acceso Incorrecto",Toast.LENGTH_LONG).show();
          Limpiar();
        break;
    }
  }
}
    
answered by 15.05.2017 в 23:01
0

First of all, all activities must be declared in the manifest. In this case LoginActivity must be the activity that is launched first (LAUNCHER):

<application ....      
    <activity android:name=".MainActivity"
        android:label="@string/title_activity_main"
        android:theme="@style/AppTheme.NoActionBar" />
    <activity
        android:name="LoginActivity"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity> 

To the code to start a new activity you have to add finish(); to that the LoginActivity is destroyed and start the MainActivity . But in addition, checking the Android documentation link you must add certain flags:

Intent main = new Intent(LoginActivity.this, MainActivity.class);
main.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
Intent.FLAG_ACTIVITY_CLEAR_TASK); 
startActivity(main);
finish();

The FLAG_ACTIVITY_CLEAR_TASK destroys the whole task associated with the activity or whatever is in the stack (stack) of that activity, therefore it will not be possible to return to the LoginActivity when pressing the return button. FLAG_ACTIVITY_NEW_TASK this activity, in this case MainActivity, will be the start of a new task in the stack.

    
answered by 16.05.2017 в 02:25
0

I have a login very similar to yours in my app, the only difference is that when I create the Intent I do it with getApplicationContext. Test to see if it can be your solution.

Intent intent = new Intent(getApplicationContext(),Slide_Menu_Activity.class);
startActivity(intent);
finish();

Always remember to use finish () so that the user does not return to the login by mistake and destroy the activity and thus free memory.

    
answered by 17.05.2017 в 09:30
0

I think you should save the reference of your Activity in a global variable. You are using the context that the subclass handles.

Assuming you have your Activity in a variable activity :

activity.startActivity(main);
finish();

Or try:

LoginActivity.this.startActivity(main);
finish();

Let me know if it works for you, I think there's the error.

    
answered by 16.05.2017 в 02:38