When I run the app itself, there is no such behaviour - once AsyncTask
finishes executing, it returns to my MainActivity (from where the AsyncTask
was started in the 1st place).
However, in my test case, the MainActivity is just suddenly paused, stopped and destroyed while the AsyncTask
just started executing for a short time.
How do I make sure that the activity waits for the AsyncTask
to call it back just like how my app really behaves?
02-21 17:23:13.063: W/MainActivity(16685): onCreate
02-21 17:23:13.133: W/MainActivity(16685): refreshRequired
02-21 17:23:13.233: W/MainActivity(16685): user is not regitered
02-21 17:23:13.233: W/MainActivity(16685): onStart
02-21 17:23:13.233: W/MainActivity(16685): onResume
02-21 17:23:16.686: W/MainActivityFunctionalTest(16685): performing the button click action
02-21 17:23:17.227: W/MainActivity(16685): Submit button is clicked
02-21 17:23:17.377: W/Process(16685): doInBackground
02-21 17:23:17.377: W/RegistrationManager(16685): doRegistration
02-21 17:23:17.387: W/RegistrationManager(16685): sendJobRequest
02-21 17:23:17.487: W/RegistrationManager(16685): didOpenedAPDUConnection
02-21 17:23:17.507: W/MainActivity(16685): onPause
02-21 17:23:17.617: W/RegistrationManager(16685): sending HTTP request
02-21 17:23:17.617: W/MessageProcessor(16685): sending a HTTP request
02-21 17:23:17.627: W/HTTPRequest(16685): HTTPRequest.startAsynchronous timeout : 60000
02-21 17:23:17.627: W/HTTPRequest(16685): url is: ...
02-21 17:23:17.848: W/HTTPRequest(16685): HTTPRequest.readResponse RESP BODY = ca000000250104313030350218437573746f6d657220646f6573206e6f74206578697374210900000000
02-21 17:23:17.848: W/MessageProcessor(16685): requestFinished
02-21 17:23:17.858: W/RegistrationManager(16685): didFailedJobRequestWithResponseMsg
02-21 17:23:17.898: W/Process(16685): onPostExecute
02-21 17:38:29.057: W/MainActivity(17483): onStop
02-21 17:38:29.057: W/MainActivity(17483): onDestroy
My test class:
public class MainActivityFunctionalTest extends
ActivityInstrumentationTestCase2<MainActivity> {
public void testRegistrationFormIsSubmitted() {
final MainActivity mainActivity = (MainActivity) solo
.getCurrentActivity();
solo.assertCurrentActivity("current activity not main activity",
MainActivity.class);
final View submitBtn = solo.getView(R.id.btn_next);
assertNotNull(submitBtn);
Log.w(TAG, "performing the button click action");
solo.clickOnView(submitBtn);
}
}
MainActivity
ImageButton submitBtn = (ImageButton) findViewById(R.id.btn_next);
submitBtn.setImageResource(R.drawable.btn_submit);
// View submitBtn = findViewById(R.id.btn_next);
submitBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.w(TAG, "Submit button is clicked");
sendActivation(v);
}
});
private void sendActivation(View v) {
process = new Processor(this, this.getApplicationContext());
process.execute();
}
Processor.java
public class Processor extends AsyncTask<Void, String, Void>{
@Override
protected Void doInBackground(Void... arg0)
{
Log.w(TAG, "doInBackground");
mManager.doRegistration();
return null;
}
}
protected Void doInBackground(Void... arg0)
you should not do any UI changes not even displaying Toasts
it throws error.
So mManager.doRegistration();
if you are performing any UI changes, it will throw error,
Instead you can use it in protected void onPostExecute()
if any UI changes are there.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.