Imri Persiado
Imri Persiado

Reputation: 1889

Reset TextView values onPause()

I created a login app represented by 2 EditText (username,password) and a button. When I click login it checks of course the information and if it's wrong it prints a dialog with a try again button. When I go back to the login activity the old username and password text are still there and I want to reset it. I tried to do that on the onPause() method since when the error dialog pops up the onPause() method is generated but I failed.

Any Idea? Thanks.

edit: after your advices I decided to re-arrannge my code and declare the EditText as they should be. The result is still the same =\

public class MainActivity extends Activity 
{
    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
    private EditText username,password;

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

    this.username = (EditText)findViewById(R.id.username);
    this.password = (EditText)findViewById(R.id.password);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
@Override
public void onPause()
{
    this.username.setText("");
    this.password.setText("");

    super.onPause();
}
public void showMsg(View view)
{
    String user = this.username.getText().toString();
    String pass = this.password.getText().toString();

    if(user.equals("a") && pass.equals("a"))
    {
        Intent i1 = new Intent(this,DisplayMessageActivity.class);
        i1.putExtra(EXTRA_MESSAGE, user);
        startActivity(i1);
    }
    else
    {   
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Error!");
        builder.setMessage(R.string.failure);
        builder.setPositiveButton("Try again", null);
        builder.show();
    }
}

}

Upvotes: 0

Views: 1027

Answers (2)

Sam
Sam

Reputation: 86948

I tried to do that on the onPause() method since when the error dialog pops up the onPause() method is generated but I failed.

I have no idea what that means, but you should be able to do something like:

@Override
protected void onPause() {
    editText.setText("");
    super.onPause();
}

If this doesn't help guide you, you need to post what you have tried and the LogCat errors. Otherwise we won't be able to help...


Addition
You don't actually leave the Activity when the username and password combination is wrong, so onPause() is never called. Try this:

public void showMsg(View view)
{
    String user = this.username.getText().toString();
    String pass = this.password.getText().toString();
    this.username.setText("");
    this.password.setText("");

Or if you only want to clear the username and password in the else-block (where you create the AlertDialog).

Upvotes: 1

Marcin S.
Marcin S.

Reputation: 11191

You can set an empty EditText once you click button:

editText.setText("");

or try to set a hint attribute in the xml:

android:hint="Enter Name"

Upvotes: 0

Related Questions