Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android get activity from within anonymous class

I'm super new to Android development and Java in general. Here's the basic setup: I have a splash screen with an AsyncTask to check server availability. Following this thread I made a callback in my activity. This makes more sense than doing the work in OnPostExecute() as I want to reuse this task in different activities.

However, in my callback I do a check if the status is okay. If it is, it should launch the next activity. But from the context of my callback, I don't get how I can get my Activity reference, which I need as a parameter for the Intent.

This is the code in my Activity under OnCreate:

    //Check server status
    CheckServiceTask t = new CheckServiceTask(new OnTaskCompleted<ShaggyServiceStatus>() {
        @Override
        public void onTaskCompleted(ShaggyServiceStatus result) {
            Log.i(TAG, "Callback. Result: " + result.getStatus());
            ProgressBar pb = (ProgressBar) findViewById(R.id.splash_progress);
            pb.setVisibility(View.INVISIBLE);

            if (result.getStatusCode() == 999){
                TextView t = (TextView) findViewById(R.id.splash_status_text);
                t.setText(result.getStatus());
                return;
            }

            Intent i = new Intent(getActivity(), LoginActivity.class);
            startActivity(i);
            finish();

        }
    });

    t.execute();

The part where this fails is at getActivity(). That call is not available. Using this throws an error (which I understand, as I'm in the context of OnTaskCompleted).

For completeness, this is the interface for OnTaskCompleted:

public interface OnTaskCompleted<T> {
    public void onTaskCompleted(T result);
}

And this is the CheckServiceTask class:

public class CheckServiceTask extends AsyncTask<Void, Void, ShaggyServiceStatus>{
    private static final String TAG = "coo";

    public OnTaskCompleted<ShaggyServiceStatus> listener;

    public CheckServiceTask (OnTaskCompleted<ShaggyServiceStatus> l){
        this.listener = l;
    }

    @Override
    protected ShaggyServiceStatus doInBackground(Void... params) {
        try {
            Log.i(TAG, "Connecting to server...");
            //TODO: Make this a setting
            final String url = "https://someplace.com/status";
            RestTemplate restTemplate = new RestTemplate();
            restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
            //restTemplate.postForObject("url", bodyObject, ShaggySer.class);
            ShaggyServiceStatus sss = restTemplate.getForObject(url, ShaggyServiceStatus.class);
            Log.d(TAG, "Got the status.");
            return sss;
        } catch (Exception e) {
            //TODO: Exception handling
            Log.e(TAG, e.getMessage(), e);
        }

        //If we're here, it's not okay.
        ShaggyServiceStatus r = new ShaggyServiceStatus("Cannot connect to server", 999, "none");

        return r;
    }

    @Override
    protected void onPostExecute(ShaggyServiceStatus result) {
        super.onPostExecute(result);
        listener.onTaskCompleted(result);
    }
}
like image 866
Coo Avatar asked Aug 09 '15 10:08

Coo


2 Answers

Use CurrentClassName.this

Intent i = new Intent(CurrentClassName.this, LoginActivity.class);

getActivity(): used with Fragments

Return the Activity this fragment is currently associated with.

class.this isn't the same as this when you have nested class.

like image 90
Ahmad Sanie Avatar answered Nov 13 '22 23:11

Ahmad Sanie


If The activity that you use Called "MyActivity" then you can do the following:

MyActivity.this

This chunk of code will return the this "current" object of outer class

like image 22
Ahmad Al-Kurdi Avatar answered Nov 13 '22 22:11

Ahmad Al-Kurdi