What is the best way for an Activity to call itself?

0

The app displays random questions following as a template with QuestionsActivity , ie the user chooses true or false and will only change the question ( textView ) and the screen design will remain the same. My questions are in AsyncTask , their order is random and should not be repeated but call this AsyncTask in onCreate of my QuestionsActivity . This will give the biggest bug. How do I "recursively" access my QuestionsActivity in a way that only instantiates my AsyncTask ONE time (at the moment it opens the App)?

    
asked by anonymous 04.04.2015 / 03:26

1 answer

1

You can implement your AsyncTask class using the standard Singleton . This way, you guarantee that during the execution of your application there will be only one AsyncTask instance.

It would look something like this:

public class MySingleton {

      // Variavel estática que conterá a instancia do AsyncTask     
      private static MySingleton instance;

     // Construtor privado. Suprime o construtor público padrao.
     private MySingleton() {

     }

     // Método público estático de acesso único ao objeto!
     public static MySingleton getInstance(){

           if(instance == NULL){
                // Uma nova instancia é criada e retornada para quem está //pedindo
               instance = new MySingleton();
           }

          // Retorna o a instância do objeto
          return instance;
     }
}
    
04.04.2015 / 03:39