Search code examples
androidperformanceapp-startup

Android app too slow. Ways to accelerate?


I've just wrote my first Android app with Android Studio. It's a vocabulary trainer and it reads in a text file in my assets folder when starting, which contains all the words (until now, I have only ~1000) like this: english$japanese$category. So, I thought this shouldn't be much work, even though I have an old Samsung S2. But It takes like 10 secs to start and sometimes it crashes.

Here's the critical code:

static String word;
static String[] listOfWords;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    readWords();
    generateRandomWord();
}

public void readWords() {
    try {
        InputStream is = getAssets().open("words.txt");
        String ww = "";
        int data = is.read();
        while(data != -1){
            ww += (char) data;
            data = is.read();
        }
        listOfWords = ww.split("\n");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void generateRandomWord() {
    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView = (TextView) findViewById(R.id.text_id);

    Random random = new Random();
    int randomKey = random.nextInt(listOfWords.length-1);
    String line = listOfWords[randomKey];
    String[] parts = line.split("/");
    Log.d("Tango-renshuu", "line: "+line+" "+parts.length+" "+parts[1]);
    textView.setText(parts[1]);
    word = parts[2];
}

The same thing happens when I try to go back to that Activity from another one, even though I'm using Intent.FLAG_ACTIVITY_CLEAR_TOP Like this:

public void back(View view) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
} 

Any idea or do you think its just my device?

Thank


Solution

  • Your readWords method is fairly inefficient: you are creating a new string at every loop iteration, and you're reading the file character by character. Consider using a BufferedReader to read the strings line by line directly:

    InputStream stream = getAssets().open("words.txt");
    BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
    ArrayList<String> lines = new ArrayList<String>();
    String line;
    while ((line = reader.readLine()) != null) {
        lines.add(line);
    }
    listOfWords = lines.toArray(new String[lines.size()]);
    reader.close();
    

    If your code is still too slow after this optimization, then you should move this code to an AsyncTask so at least it doesn't freeze the UI, and you can display a loading spinner in the meantime.