Android: Problems with AsyncTask when launching items

1

I have a problem what happens is that I create an intent after I start it and at the end the MainActivity must be destroyed but all that process is marking me an error because I am in the SearchData class and not in MainActivity in the same way with the alertDialogBuilder and the txtCode are marking error because those elements belong to MainActivity then I do not know how to solve those errors I read something from Context but I am not understanding it well and I do not know if that is all this is in AsyncTask that is in a separate class.

@Override
protected void onPostExecute(CheckIn result) {//se ejecuta cuando termina doingBackgroud()
    frameLayout.setVisibility(View.GONE);

    if (result != null){
        Intent intentIndex = new Intent(MainActivity.this, IndexActivity.class);//Creo un intent
        startActivity(intentIndex);//inicio la actividad
        finish();//se destruye la actividad
    } else {
        System.out.println("primer error");
        alertDialogBuilder.setTitle("ERROR..").setMessage("El codigo no se encontro").setCancelable(false).setPositiveButton("ACEPTAR", null).show(); //mensaje si no existe el checkin
        txtCode.setText("");
    } //./else
}
    
asked by Javier fr 15.12.2016 в 18:59
source

2 answers

1

In your SearchData class you must create an interface

 public interface TaskListener {
   //Declara como parametro los elementos que necesites
   public void onPostExecute(CheckIn result);
}

Go to your MainActivity and implement this interface

 public class MainActivity implements TaskListener{
   //Sobreescribe al método

   @Override
   public void onPostExecute(CheckIn result){

       //Invoca a tu siguiente activity

    if (result != null){
      Intent intentIndex = new Intent(MainActivity.this,  IndexActivity.class);

       //Creo un intent
       startActivity(intentIndex);//inicio la actividad
       finish();//se destruye la actividad
    } else {
       Log.d("TAG", "primer error");
       alertDialogBuilder.setTitle("ERROR..").setMessage("El codigo no se encontro").setCancelable(false).setPositiveButton("ACEPTAR", null).show(); //mensaje si no existe el checkin
    txtCode.setText("");
    }
   }
 }

Your SearchData class sends you the reference of your TaskListener:

// Example

SearchData searchData = new SearchData(this); //Dependiendo de cuantos parametros tengas
searchData.execute(); 

In your SearchData class

 // Declaras a tu listener
 private TaskListener listener;

 //Constructor
 public SearchData(TaskListener listener){
    this.listener = listener;
 }

On the onPostExecute inside your SearchData You declare the following:

 @Override
 public void onPostExecute(CheckIn result){
    if(listener != null){
     listener.onPostExecute(result);
    }
 }

I hope it serves you

Greetings.

    
answered by 15.12.2016 в 19:37
0

In my class MainActivity.java I call my AsyncTask and I pass it as parameter my Context

SearchData search = new SearchData(MainActivity.this);
search.execute(...);

Finally in the constructor of my SearchData that is where I have the AsyncTask I get it

public SearchData(Context context) {
    mContext = context;
} 
    
answered by 16.12.2016 в 02:03