0

I have 2 AsyncTasks running in the background after the app statrs. The first is getting the location, and after it finishes, it calls the other task. After the second task finishes, it should update the UI in the postexecute, but it doesn't. It should fill the recyclerview with data, hide the loading icon and display the recyclerview.

    private FusedLocationProviderClient fusedLocationClient;
    Location location;
    RecyclerView recV;

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

    findViewById(R.id.loading).setVisibility(View.VISIBLE);

    recV = findViewById(R.id.rv);
    recV.setLayoutManager(new LinearLayoutManager(MainActivity.this));


    new getLocation().execute(); //the first task gets called here

}

Here's the first asynctask requesting location:

class getLocation extends AsyncTask<String, Boolean, Boolean> {
    @Override
    protected Boolean doInBackground(String... params) {
        fusedLocationClient = LocationServices.getFusedLocationProviderClient(MainActivity.this);

        if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {}
        CancellationTokenSource cancellationSource = new CancellationTokenSource();
        fusedLocationClient.getCurrentLocation(LocationRequest.PRIORITY_HIGH_ACCURACY, cancellationSource.getToken())
                .addOnSuccessListener(MainActivity.this, new OnSuccessListener<Location>() {
                    @Override
                    public void onSuccess(Location loc) {
                        location = loc;

                        publishProgress(true);
                    }
                }).addOnFailureListener(MainActivity.this, new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                e.printStackTrace();
                publishProgress(true);
            }
        });

        while(true){
            if(location != null)
                return true;
        }
    }

    @Override
    protected void onPostExecute(Boolean result) {
        if(result){
            new getData().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);  //Second task called here

            //Updating the UI here doesn't work

        }
    }
}

Editing anything on the UI here is not working as well. It gets called, logging to console works, but modifying the UI doesn't do anything here.

Same thing happens with the second task as well.

runOnUiThread doesn't do anything as well.

class getData extends AsyncTask<String, Boolean, Boolean> {
    @Override
    protected Boolean doInBackground(String... params) {...}
    
    @Override
    protected void onPostExecute(Boolean result) {
        Log.d(TAG, "this gets displayed in the logcat");
        RecyclerView_Adapter adapter = new RecyclerView_Adapter(routes);
        recV.setAdapter(adapter);
        findViewById(R.id.loading).setVisibility(View.GONE);
        findViewById(R.id.rv).setVisibility(View.VISIBLE);
        findViewById(R.id.rvui).setVisibility(View.VISIBLE);

        //But these UI tasks just don't do anything

What's wrong with this code? Any help is appreciated!

SGeri
  • 29
  • 9

1 Answers1

0

This code is just horrible. If getCurrentLocation returns null (which it can), your loop will never exit, because location never changes- its either always null or always not null. Most likely you're not getting any location, thus looping forever. And since all AsyncTasks share a single thread, this will block all async tasks in your app.

Remove the loop, replace it with return return location != null. And make sure any code in onPostExecute can handle it if location is null.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127