Topic doesn't end right I guess

public String newUser = "false";
public double lat = 0.0, lon = 0.0;

      

I have the following function in my Android application (called on button click) that starts a thread:

public void SignUpFunction(View view) {
    assignValues();
    String filledAll = checkIfFilled();
    if (filledAll.equals("true")) {
    Log.d("LIFECYCLE", "calling thread..");

    //my thread
    new validateThread().start();

    Log.d("After thread start","This log call does not occur");

    if (newUser.equals("true")) {
        Toast.makeText(this, "Please wait as we obtain your location", Toast.LENGTH_SHORT).show();
        getMyLocationFunction();
        } else {
            return;
        }
    }
}

      

validateThread:

class validateThread extends Thread {
    public void run() {
        Log.d("LIFECYCLE", "validateThread entered...");
        try {
                        newUser = "true";
                        Log.d("validateThread", "Validated and found new user");
        } catch (Exception e) {
            Log.d("validateThread", "Exception in validateThread: " + e.getMessage());
        }
    }
}

      

The stream is working correctly ... but after the last line it does not go back to the starting point. I don't understand why this is happening because I have used streams before and they all work correctly.

I know I can just give the getMyLocation function inside the thread, but I really need it.

I've searched for similar questions but none helped. What am I doing wrong here? Thanks in advance.

+3


source to share


1 answer


It's a race. SignUpFunction should wait for validateThread to decide whether to set newUser = "true". Even with race, your code might work sometimes, but that's by accident.



+2


source







All Articles