Android gets activity from an anonymous class

I am super new to Android and Java development in general. Here is the basic setup: I have a splash screen with AsyncTask to check server availability. After this topic, I made a callback in my activity. This is more important than doing the work in OnPostExecute() , because I want to reuse this task in different actions.

However, in my callback I check if the status is ok. If so, he should start the next operation. But from the context of my callback, I don’t understand how I can get the Activity link that I need as a parameter for Intent.

This is the code in my OnCreate activity:

  //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 in getActivity() . This call is not available. Using this causes an error (which I understand, since I am 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 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); } } 
+7
java android android-asynctask
source share
2 answers

Use CurrentClassName.this

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

getActivity() : used with fragments

Returns the action this fragment is currently associated with.

class.this does not match this when you have a nested class.

+11
source share

If the action you are using is called " MyActivity ", you can do the following:

 MyActivity.this 

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

+4
source share

All Articles