0

I have a Button which code is like this:

public void buttonRegister(View view){
    EditText etUsername = (EditText) findViewById(R.id.etUsername);
    EditText etPassword = (EditText) findViewById(R.id.etPassword);
    final String username = etUsername.getText().toString();
    final String password = etPassword.getText().toString();


    User newUser = new User(username, password);
    newUser.save(new StackMobModelCallback() {

        @Override
        public void failure(StackMobException arg0) {
            // TODO Auto-generated method stub
            System.out.println("fail");
            Toast.makeText(getApplicationContext(), "Fail", Toast.LENGTH_SHORT).show();

        }

        @Override
        public void success() {
            // TODO Auto-generated method stub
            System.out.println("success");
            Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
        }
    });
}

But the app is not showing the Toast... what can I do to fix this?

Cœur
  • 37,241
  • 25
  • 195
  • 267
ollidiemaus
  • 279
  • 2
  • 4
  • 9

1 Answers1

1

You probably need to execute the Toast code in the UI tread. Something like this:

public void buttonRegister(View view){
    EditText etUsername = (EditText) findViewById(R.id.etUsername);
    EditText etPassword = (EditText) findViewById(R.id.etPassword);
    final String username = etUsername.getText().toString();
    final String password = etPassword.getText().toString();


    User newUser = new User(username, password);
    newUser.save(new StackMobModelCallback() {

        @Override
        public void failure(StackMobException arg0) {
            // TODO Auto-generated method stub
            System.out.println("fail");
            YourActivity.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(YourActivity.this, 
                                    "Fail", 
                                    Toast.LENGTH_SHORT).show();
                }
            }                
        }

        @Override
        public void success() {
            // TODO Auto-generated method stub
            System.out.println("success");
            YourActivity.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(YourActivity.this, 
                                    "Success", 
                                    Toast.LENGTH_SHORT).show();
                }
            }
        }
    });
}
Luiz Aoqui
  • 271
  • 4
  • 13
  • Thank you that worked now i just need to find out why this is not running on UI Thread by Default... – ollidiemaus Dec 12 '13 at 15:42
  • 1
    The StackMob is probably running on a background thread so it won't block the UI while it waits for the response. On Android is always a good practice to never do long-time processing on the main thread because this will affect the user experience. – Luiz Aoqui Dec 12 '13 at 17:08