Home > Software engineering >  How to move from activity to another with condition?
How to move from activity to another with condition?

Time:10-11

I need to go from an activity to another only if the condition was true If the condition was false I need it to stay in the same activity

the true condition is working and it is moving me to the second activity While the (false) is closing the whole program and don't stay in the activity, how can I prevent that? My code is as follows (below -else- I added some ways but they didn't work at all):

Intent move = new Intent(this, MainActivity.class);

    if(minString != null && maxString !=null) {
        startActivity(move);

    }
    else {
       // minString.setError( "fill the field!" );

        //Toast.makeText(getApplicationContext(), "fill the field", Toast.LENGTH_SHORT).show();

        //Intent move = new Intent(this, FirstActivity.class);
       // startActivity(move);
    }

CodePudding user response:

assuming that "minstring" and "maxstring" are inputs.

try using this code inside your button

     EditText minstring = (EditText) findViewById(R.id.YOUR_ID);
    EditText maxstring = (EditText) findViewById(R.id.YOUR_ID);
    String strMinString = minstring.getText().toString();
    String strMaxString = maxstring.getText().toString();

Intent move = new Intent(this, MainActivity.class);

if(TextUtils.isEmpty(strMinString) && TextUtils.isEmpty(strMaxString) ) {
    minstring.setError("Your message");
    maxstring.setError("Your message");

    return;
 }else if(minString != null && maxString !=null) {
        startActivity(move);

    }
  • Related