He intentado guardar el estado de mi asynctask y creo que lo que conseguido (ahora añadiré el código de esta y pediría por favor que me lo confirmen también), pero cuando hago la rotación de la pantalla, pierdo todo lo que hay en esta, no se visualiza en el layout de la mainActivity.
MainActivity
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private Button btnNormalFile, btnLargeFile;
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private ProgressBar progressBar;
private AsyncTaskHolder asyncTaskHolder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
inflateViews();
}
public void initViews(){
toolbar = (Toolbar) findViewById(R.id.toolbar);
btnNormalFile = (Button) findViewById(R.id.btnNormalFile);
btnLargeFile = (Button) findViewById(R.id.btnLargeFile);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
recyclerView = (RecyclerView) findViewById(R.id.rvPeople);
layoutManager = new LinearLayoutManager(getApplicationContext());
}
public void inflateViews(){
setSupportActionBar(toolbar);
btnNormalFile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//new TaskLoadInfo(recyclerView, progressBar, getApplicationContext(), btnLargeFile, btnNormalFile).execute("normal");
asyncTaskHolder.execute("normal");
}
});
btnLargeFile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//new TaskLoadInfo(recyclerView, progressBar, getApplicationContext(), btnLargeFile, btnNormalFile).execute("large");
asyncTaskHolder.execute("large");
}
});
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setHasFixedSize(true);
asyncTaskHolder = getAsyncTaskHolder();
}
private AsyncTaskHolder getAsyncTaskHolder() {
if (this.asyncTaskHolder != null) {
return asyncTaskHolder;
}
Object instance = getLastCustomNonConfigurationInstance();
if (instance == null) {
instance = new AsyncTaskHolder(recyclerView, progressBar, getApplicationContext(), btnLargeFile, btnNormalFile);
}
if (!(instance instanceof ActivityDependant)) {
Log.e("", instance.getClass().getName() + " must implement ActivityDependant");
}
return (AsyncTaskHolder) instance;
}
@Override
public Object onRetainCustomNonConfigurationInstance() {
return asyncTaskHolder;
}
@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);
}
@Override
protected void onStart() {
super.onStart();
if (asyncTaskHolder != null) {
asyncTaskHolder.attach(this);
}
}
@Override
protected void onStop() {
super.onStop();
if (asyncTaskHolder != null) {
asyncTaskHolder.detach();
}
}
AsyncTask
public class AsyncTaskHolder implements ActivityDependant{
private Activity parentActivity;
private boolean isRunning;
private boolean isUpdateOnAttach;
private RecyclerView recyclerView;
private ProgressBar progressBar;
private Context context;
private PeopleAdapter peopleAdapter;
private Button btnLarge, btnNormal;
private UIUtils utils;
public AsyncTaskHolder(RecyclerView recyclerView, ProgressBar progressBar, Context context, Button large, Button normal) {
this.recyclerView = recyclerView;
this.context = context;
this.progressBar = progressBar;
this.btnLarge = large;
this.btnNormal = normal;
}
@Override
public void attach(Activity activity) {
this.parentActivity = activity;
if (isUpdateOnAttach) {
isUpdateOnAttach = false;
}
}
@Override
public void detach() {
this.parentActivity = null;
}
public synchronized void execute(String file) {
if (isRunning) {
Toast.makeText(parentActivity, "Already running", Toast.LENGTH_SHORT).show();
return;
}
isRunning = true;
new AsyncTask<String, People, Void>() {
@Override
protected void onPreExecute() {
utils = new UIUtils(context);
peopleAdapter = new PeopleAdapter();
recyclerView.setAdapter(peopleAdapter);
progressBar.setVisibility(View.VISIBLE);
utils.actionOperation("Initiating the operation.", false, btnLarge, btnNormal, R.color.year);
}
@Override
protected Void doInBackground(String... file) {
InputStream flujo = null;
try
{
if(file[0].equals("normal"))
{
flujo = context.getResources().openRawResource(R.raw.nombres);
}
else
{
flujo = context.getResources().openRawResource(R.raw.nombresgrande);
}
BufferedReader lector = new BufferedReader(new InputStreamReader(flujo));
String texto = lector.readLine();
while(texto != null)
{
publishProgress(new People(texto));
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
texto = lector.readLine();
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{
if (flujo != null)
{
flujo.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(People... values) {
if (peopleAdapter.checkNameWithList(values[0])){
peopleAdapter.addPerson(values[0]);
}
peopleAdapter.notifyDataSetChanged();
}
@Override
protected void onPostExecute(Void aVoid) {
progressBar.setVisibility(View.GONE);
utils.actionOperation("Operation finished.", true, btnLarge, btnNormal, R.color.colorPrimary);
}
}.execute(file);
}
Gracias de antemano!