I have a splashcreen where I run a background process with AsyncTask
, when the device is broken the previous task continues, but it starts again.
I am reading about how to face the problem, in the best way possible, that is, avoid bad habits / practices, such as putting the activity in persistent mode or capturing android:configChanges
with AndroidManifest
, the best method that indicate is to use weakReference
but it is not like implementing it with the AsyncTask.
I would also like to show a ProgressDialog
but I also find that if the device is broken it disappears.
I'm looking at myself: retaining-objects-across-config-changes
I have the following to do the tests:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private myAsyncTask handlerTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
handlerTask = new myAsyncTask();
handlerTask.execute();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void largeTask() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
//e.printStackTrace();
}
}
private class myAsyncTask extends AsyncTask<Void, Integer, Boolean> {
@Override
protected Boolean doInBackground(Void... params) {
//Log.i(TAG, "doInBackground: ");
for (int i = 1; i <= 10; i++) {
largeTask();
Log.d(TAG, "doInBackground: " + i);
publishProgress(i * 10);
if (isCancelled()) {
Log.w(TAG, "isCancelled: TRUE");
break;
}
}
return true;
}
@Override
protected void onProgressUpdate(Integer... values) {
//Log.i(TAG, "onProgressUpdate: ");
int progreso = values[0];
Log.d(TAG, "onProgressUpdate: " + values[0]);
}
@Override
protected void onPreExecute() {
Log.i(TAG, "onPreExecute: ");
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
Log.i(TAG, "onPostExecute: ");
if (result) {
//pDialog.dismiss();
Toast.makeText(MainActivity.this, "Tarea finalizada!", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onCancelled() {
Log.w(TAG, "onCancelled: ");
Toast.makeText(MainActivity.this, "Tarea cancelada!", Toast.LENGTH_SHORT).show();
}
}
}
Each time you press the fabButton
you execute the task, if you rotate the device and press again the fabButton
launches another task, if you were in onCreate just rotate it would launch again, is what I want avoid, also add a dialogFragment
with horizontal progress.