Home » Java » Java Arrays.fill() function executes prematurely on main thread?

Java Arrays.fill() function executes prematurely on main thread?

Posted by: admin June 30, 2018 Leave a comment


This code is under a button’s OnClickListner

String username = edtLabel.getText().toString():
char[] password = new char[edtPassword.getText().length()];
edtPassword.getText().getChars(0, edtPassword.getText().length(), password,0);

new BackGroundTask(username, password).execute();
Arrays.fill(password, '0');

The problem I am facing here is that the password passed to the BackGroundTask constructor(an AsyncTask class) is always an array of zeros that is {0, 0, 0}. Although I call Arrays.fill() function after the setting up the constructor.

The code is a bit longer than the given snippet, validating several other things but does not alter password at any point of time.

I have been looking everywhere for quite sometime but could not find anything.
Only solution I see is using a Handler which executes after a few hundred milliseconds which fills the passwords with zeros.

new Handler().postDelayed(new Runnable() {
    public void run() {

        Arrays.fill(password, '0');
 }, 100);

Any better and straight forward technique would be great, Thanks.