Question

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?

Was it helpful?

Solution

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();
                }
            }
        }
    });
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top