Skip to content Skip to sidebar Skip to footer

Intent Does Not Work In Case Of If Else

Here is my sample code: String checkin = imageUpload.getStatus(); if (checkin.equals('1')) { Toast.makeText(FullImageActivity.this, name+' '+ 'Checked In', Toast.LENGTH_LONG).

Solution 1:

In addition to the answers here ,it seems that this issue is by multiple problems. As some here pointed out to take FullImageActivity.this instead of getApplicationContext(), my assumption was that it was not rebuild correctly. So for everybody who has the same problem: On some devices(eg. Huawei Ascend mate 7) , Android Studio doesn´t install the new version after an update correctly althought it shows this in the Run tab.

The best way to update after some code changes is to uninstall from device, clean project and reinstall your app.

So, this answer is not a code example (this part about the context is allready said) but important and fixed the problem of the questioner.

Solution 2:

SO try using it in Switch case :

int checkin = Integer.parseInt(imageUpload.getStatus());
                switch(checkin) 
                 {
                 case1:
                    Toast.makeText(FullImageActivity.this, name+" "+ "Checked In", Toast.LENGTH_LONG).show();
                    Intent i = new Intent(FullImageActivity.this, Activiy_1.class);
                    startActivity(i);
                    //openCamera.setText("CheckOut");break;
                case0:    
                    Toast.makeText(FullImageActivity.this, name + " " + "Checked Out", Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(getApplicationContext(), Activiy_0.class);
                    startActivity(intent);''break;
                default:
                    //default methods

                }

Hope this works for you....

EDITED ::

If string works then

Stringcheckin= imageUpload.getStatus();
                switch(checkin) 
                 {
                 case"1":
                    Toast.makeText(FullImageActivity.this, name+" "+ "Checked In", Toast.LENGTH_LONG).show();
                    Intenti=newIntent(getActivity().this, MainActivity.class);
                    startActivity(i);
                    //openCamera.setText("CheckOut");break;
                case"0":    
                    Toast.makeText(getApplicationContext(), name + " " + "Checked Out", Toast.LENGTH_LONG).show();
                    Intentintent=newIntent(getActivity().this, MainActivity.class);
                    startActivity(intent);''break;
                default:
                    //default methods

                }

Solution 3:

This lines of Code worked for me. We dont need to create two intents for if else. Just create one and call the startactivity after if else. That is it.

ImageUploadimageUpload= response.body();
    Intenti=newIntent(FullImageActivity.this, MainActivity.class);
    Stringcheckin= imageUpload.getStatus();
    if (checkin.equals("1")) {
    Toast.makeText(FullImageActivity.this, name+" "+ "Checked In", Toast.LENGTH_LONG).show();
         } else {
    Toast.makeText(FullImageActivity.this, name + " " + "Checked Out", Toast.LENGTH_LONG).show();
               }

  startActivity(i);

Solution 4:

Assuming imageUpload is an asynch task instance, you forgot to test for the PENDING Status.

So the final result would look something like this:

Stringmessage="";
Stringcheckin= imageUpload.getStatus();
switch(checkin) {
      case Status.FINISHED:
             message = "Checked In";
             break;
       case Status.RUNNING:    
             message = "Checked Out";
             break;
       case Status.PENDING:    
             message = "Trying";
             break;
        default:
             message = "Arghhhh";  
 }

 Toast.makeText(FullImageActivity.this, name + " " + message,  Toast.LENGTH_LONG).show();
 Intenti=newIntent(FullImageActivity.this, Activiy_1.class);
 startActivity(i);

// You'll have to test this yourself. I didn't test it. 

Ideally, it's better to use static final variables for the status strings, since those never change and this way you don't have to remember what their values mean.

Also if you really start the same Activity whatever happens, then that's duplicate code, and you can do that part outside of the switch statement.

The same goes for showing the Toast. It seems to me like only the content of the Toast changes, so only that content needs to be in the switch.

Post a Comment for "Intent Does Not Work In Case Of If Else"