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

Asked

Viewed 157 times

0

The app displays random questions by following as template to QuestionsActivity, that is, 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 a AsyncTask, their order is random and should not be repeated but I call this Asynctask no onCreate of my QuestionsActivity. This will give the biggest bug. How do I access "recursively" my QuestionsActivity in a way that only instates my Asynctask ONCE (at the moment that opens the App)?

  • 1

    What you can do is instead of having to create a new Activity (or recreate it) for each screen (question), if the layout and functions (features) of the application is the same for all questions and only exchange the content of the question, you may not recreate the whole Activity, but rather draw the new issue restore original screen state and play values.

1 answer

1

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

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;
     }
}
  • I’m sorry, I don’t quite understand the question. Do you want to know if Arraylist will always stay in the same configuration? That is, regardless of performing a shuffle on it, it will always remain in the initial state?

  • I removed the previous one because it wasn’t clear. Open the app and Activity will be initialized, click on the button and a new object will be made from that same Activity (the previous one will be cleaned by garbageCollection). Asynctask is accessed by Activity itself, that is, despite Singleton a new instance will be made because the previous one no longer exists. (Not two in the same execution)

  • Perhaps the best way is to get all the questions and change the textView as the user clicks on the "next" button. Thank you, I voted to close this issue!

  • 1

    Ok, but anyway you should take into account that regardless of whether you destroy Activity or not, only one instance of Asynctask will exist.

  • In that case, will there be no Asynctask object? Since the Asynctask object was Activity object and was destroyed

  • 1

    Because Asynctask is a static variable of the Singleton class, there will always be a single Asynctask variable and it will be the same for all Singleton class objects. That way, if you create an "A" Singleton object, set an Asynctask for it and then train "A", Asynctask will not be destroyed, after all, as was said, Asynctask is static.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.