Answer the question
In order to leave comments, you need to log in
Long running asynchronous operations in Android?
It is necessary to implement a "login form" in the application. A simplified model looks like this:
1. the user presses a button
2. a "long" operation is launched
3. after the operation, the user is transferred to some kind of activity
How to implement this correctly?
Known nuances of the Android platform:
1. Operations not directly related to updating the UI must be performed outside the UI thread.
2. The user can leave the activity at any time, including "immediately after the start of the operation."
3. The user can return to the activity at any time, including "before the operation is completed" and "after the operation is completed"
4. If the user left the activity, there are no guarantees
(To simplify the task, let's assume that after clicking the button, we immediately show a ProgressDialog that cannot be closed, so the Back scenario can be skipped)
Requirements:
If the user clicked the button, then without any "assume" and "if" the operation should work and only the result is available, the user should be transferred to another activity. This behavior should not depend on what the user did immediately after pressing the button: received an incoming call, turned the device, pressed Home, deciding to read Twitter, etc.
1. There is a known simple wrong solution like this:
...
new AsyncTask() {
void doWork() {
boolean authenticated = webService.authenticate(email, password);
if(authenticated) {
Intent intent = new Intent(AuthActivity.this, HomeActivity.class);
startActivity(intent);
finish();
}
}
}
...
class WebServiceFacade extends IntentService {
... // оно пришлёт нам Broadcast
}
...
// дёргаем сервис
Intent authenticateIntent = new Intent(...)
startService(authenticateIntent);
...
// ждём ответ от сервиса
BroadcastReceiver receiver = .... {
Intent intent = new Intent(AuthActivity.this, HomeActivity.class);
startActivity(intent);
finish();
}
(at)Singleton
class MyService {
...
}
...
(at)Inject
private MyService service;
...
void onResume() {
if(service.hasAuthResult()) {
Intent intent = new Intent(AuthActivity.this, HomeActivity.class);
startActivity(intent);
finish();
} else {
service.setListener(this);
}
}
...
void onPause() {
service.setListener(null);
}
...
void MyServiceListener.onAuthResult(bool isAuthenticated) {
Intent intent = new Intent(AuthActivity.this, HomeActivity.class);
startActivity(intent);
finish();
}
...
Answer the question
In order to leave comments, you need to log in
You have described all this in such detail that there is nothing to object to. You have such requirements that it is easier, apparently, not to do. You probably know that Android is full of solutions of this kind: complex and sweeping. For example when working with XML. So I think you should leave it like that.
You are too categorical about AsyncTask. AsyncTask doesn't have to run another activity, it should have one simple task. In your case: send a request > receive a response > store in the database the data that the user is authorized or not > notify those interested that the request was successful / not successfully completed. Then there are only two scenarios:
1. If the user is still in activity authorization, then the next activity is launched
2. If the user is somewhere else, then when returning to the application, the onResume method will be called in which the database must be checked for authorization.
PS The same story, by the way, with IntentService. He should just execute the request and save the result in the database, he should not broadcast the event ala "authorization was successful, launch the specific activity"
PS2 ProgressDialog without the possibility of canceling it in this case bad manners, because. the user could just accidentally press the button, or he is in a bad network zone and he will have to watch for 20-30 seconds until the request is completed.
I would do this with AsyncTask. The reason is simple, because you don't want the user to notice that some additional service is running there ...
And the dialogs - all the blocking dialogs in android - are insanity.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question