-1

I'm new to java and android so I am asking these kind of questions here to understand how it should work.

I have 2 Activities in my app viz Welcome_Activity.java and Content_Activity.java

The First Activity work smoothly but the problem comes when the Content_Activity(Second Activity) is called from Welcome_Activity(First Activity)

In the onCreate of Second Activity, I think the contents which are too many cause the Activity to load too slow.

How to solve this issue?

Example :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_Puzzle_game);

    q1_tv = (TextView) findViewById(R.id.q1_tv);
    q2_tv = (TextView) findViewById(R.id.q2_tv);
    q3_tv = (TextView) findViewById(R.id.q3_tv);
    q4_tv = (TextView) findViewById(R.id.q4_tv);
    q5_tv = (TextView) findViewById(R.id.q5_tv);
    q6_tv = (TextView) findViewById(R.id.q6_tv);
    q7_tv = (TextView) findViewById(R.id.q7_tv);
    q8_tv = (TextView) findViewById(R.id.q8_tv);
    q9_tv = (TextView) findViewById(R.id.q9_tv);
    q10_tv = (TextView) findViewById(R.id.q10_tv);

    bt1 = (Button) findViewById(R.id.bt1);
    bt2 = (Button) findViewById(R.id.bt2);
    bt3 = (Button) findViewById(R.id.bt3);
    bt4 = (Button) findViewById(R.id.bt4);
    bt5 = (Button) findViewById(R.id.bt5);
    bt6 = (Button) findViewById(R.id.bt6);
    bt7 = (Button) findViewById(R.id.bt7);
    bt8 = (Button) findViewById(R.id.bt8);
    bt9 = (Button) findViewById(R.id.bt9);
    bt10 = (Button) findViewById(R.id.bt10);

    SP = getSharedPreferences("saved_data", MODE_PRIVATE);
    solved = SP.getInt("solved", 0);

    // LoadLevel & ResumeGame is just a method to get data from another class that have array Strings to fill the textviews and buttons 

    if (solved > 0) {
        ResumeGame(null);
    }
    else {
        LoadLevel(null);
    }
}
Komal12
  • 3,340
  • 4
  • 16
  • 25
Fadi A.
  • 93
  • 1
  • 10

2 Answers2

-1

You should have a look in Asyntask. It helps you to load/do the task that take sometime, by executing it in background thread. That would help your Activity(UI) won't be jerky.

nđq
  • 388
  • 5
  • 14
  • gotta do some research about this Asyntask , as i mentioned i'm a beginner ..so XD thanks a lot man – Fadi A. Aug 18 '17 at 05:58
-1

I assume you are fetching data in ResumeGame & LoadGame methods either from web services or from local app storage; if so try to do fetching thing in other than main thread (UI thread) and as soon as you get the data try to pass this data on UI thread. The best way to do it for you (beginner) use AsyncTask, usable callbacks in AsyncTask could be:

  • doInBackground: here write your code to get the data
  • onPostExecute: Here you can fill your UI views like button label, text of TextView

I hope this will help you in order to improve performance.

Anshuman Jaiswal
  • 5,352
  • 1
  • 29
  • 46
  • it's just a class named "questions" with strings array for each level .. so i'm loading data from a class i just pick 10 elements from the array that inside that class – Fadi A. Aug 18 '17 at 06:33
  • If you are not using any data manipulation/fetch, I guess it's your layout that takes time to render. You should look into your layout files and optimize layouts by using less number of containers. – Anshuman Jaiswal Aug 18 '17 at 06:52
  • it could be, i guess i will try splash screen then , thanks a lot Anshuman – Fadi A. Aug 18 '17 at 06:57