0

When I start my actvity, there is a moment of waiting before i can press buttons.

I'm a beginner and i want progress.

I think the problem comes from the initialization of the actvity.

Here is the beginning of my activity. The rest are just listenners.

public class Questions extends AppCompatActivity{

    SharedPreferences sharedPreferences;
    int Question;
    int Categorie;
    int Reponse;
    int Points_question;

    int REPONSE[]= new int[5]; //tableau des reponses
    int REPONSE_POINTS[][] = {{1,2,3,4},{2,3,4,5},{1,2,3,4},{2,3,4,5},{1,2,3,4},{2,3,4,5}}; //tableau fixe des points de reponses
    int REPONSE_CATEGORIE[] = {1,2,2,1,2}; //Categories de chaque questions

    ArrayList CATEGORIE1 = new ArrayList(); //Tableau categories 1
    ArrayList CATEGORIE2 = new ArrayList(); //Tableau categorie 2


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.questions);

        //chercher les views
        CardView cardView1 = findViewById(R.id.cardview1);
        CardView cardView2 = findViewById(R.id.cardview2);
        CardView cardView3 = findViewById(R.id.cardview3);
        CardView cardView4 = findViewById(R.id.cardview4);
        final TextView reponse1 = findViewById(R.id.reponse1);
        final TextView reponse2 = findViewById(R.id.reponse2);
        final TextView reponse3 = findViewById(R.id.reponse3);
        final TextView reponse4 = findViewById(R.id.reponse4);
        final TextView questions = findViewById(R.id.questions);

        //SAVE
        sharedPreferences = getPreferences(MODE_PRIVATE);
        Question = sharedPreferences.getInt("num", 0);
        
        //Mettre en forme pour la recuperation de la question
        String Question_string = Integer.toString(Question);
        String mnemonic_q = "q_";
        String mnemonic_question = mnemonic_q + Question_string;

        //Mettre en forme pour la recuperation des reponses
        String mnemonic_r = "r_";
        String mnemonic_reponse1 = mnemonic_r + Question_string + "1";
        String mnemonic_reponse2 = mnemonic_r + Question_string + "2";
        String mnemonic_reponse3 = mnemonic_r + Question_string + "3";
        String mnemonic_reponse4 = mnemonic_r + Question_string + "4";

        //Recuperer String
        int id = getResources().getIdentifier(mnemonic_question, "string", getPackageName());

        int r_1 = getResources().getIdentifier(mnemonic_reponse1, "string", getPackageName());
        int r_2 = getResources().getIdentifier(mnemonic_reponse2, "string", getPackageName());
        int r_3 = getResources().getIdentifier(mnemonic_reponse3, "string", getPackageName());
        int r_4 = getResources().getIdentifier(mnemonic_reponse4, "string", getPackageName());

        //L'ecrire
        questions.setText(id);

        reponse1.setText(r_1);
        reponse2.setText(r_2);
        reponse3.setText(r_3);
        reponse4.setText(r_4);
        
      

thank you in advance !

Leic
  • 57
  • 8

1 Answers1

0

There is always a gap in starting the app, but to make your app load faster ( at least show UI to user) you can put everything after //SAVE to another thread.

handler = new Handler();
Runnable r = new Runnable() {
    public void run() {
        //SAVE ...
    }
};

Read more here

Jafar Akhondali
  • 1,552
  • 1
  • 11
  • 25