Hi!
I have an activity where I have to do som background tasks. I am using a observable/subscriber with rxandroid. Just before starting the background task I show a progressdialog and when the job is done I dissmiss it. Problem is, if I rotate the screen, progressdialog dissapear. What I want is to keep showing progressdialog until the background task finishs.
This is my code:
I have an activity where I have to do som background tasks. I am using a observable/subscriber with rxandroid. Just before starting the background task I show a progressdialog and when the job is done I dissmiss it. Problem is, if I rotate the screen, progressdialog dissapear. What I want is to keep showing progressdialog until the background task finishs.
This is my code:
Java:
private Subscriber suscriptor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
suscriptor = new Subscriber() {
@Override
public void onCompleted() {
progressDialog.dismiss();
Log.d("SUSCRIPTOR","ON COMPLETE");
}
@Override
public void onError(Throwable e) {
Log.d("SUSCRIPTOR","ON ERROR");
}
@Override
public void onNext(Object o) {
Log.d("SUSCRIPTOR","ON NEXT");
}
};
//at some point when a button is clicked
reset_database.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
ACTION = LOAD_DATA;
progressDialog = ProgressDialog.show(getActivity(), getString(R.string.dialog_carga_configuracion_background),getString(R.string.dialog_carga_porfavor_espere), false, false);
backgroundTask();
return true;
}
});
private void backgroundTask()
{
setRetainInstance(true);
Observable.create(
new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> sub) {
accionBackground();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
sub.onNext("cargar");
sub.onCompleted();
}
@Override
protected void finalize() throws Throwable {
super.finalize();
Log.d("OBSERAVBLE","FINALIZADO");
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.newThread())
.cache()
.subscribe(suscriptor);
@Override
public void onDestroy()
{
if(suscriptor!=null)
{
if(!suscriptor.isUnsubscribed())
{
suscriptor.unsubscribe();
}
}
super.onDestroy();
}
When I rotate screen is not crashing but I am not sure it is finishing job completely.
Any ideas how to do what I want?
Thanks!