1

I am currently developing kind of a Database application for android. However, since the amount of entries is quite low, I don't feel like I need to use a Sqlite database. Now, I currently have the "reading" from the save file in the onCreate() method. But this also loads the file new when I turn the phone from horizontal to vertical display mode, which though it does not affect performance in a huge way, since I don't have that many entries, still bothers me, as I would like to do it the proper way.

What would the "correct" way to do this be?

private TableLayout tblLayout;

private ArrayList<Entry> listOfEntries;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    tblLayout = new TableLayout(this);

    // this basically populates my ArrayList
    listOfEntries = readFromFile(saveFile);

    setContentView(tblLayout);
}
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
Adrian Jandl
  • 2,985
  • 4
  • 23
  • 30

3 Answers3

1

Add following to your to your Activity's manifest node:( where you've declared the activity in the AndroidManifest file):

android:configChanges="keyboardHidden|orientation|screenSize"

It will prevent from calling the onCreate() again, when the device rotates. This way you will prevent from reading it again on orientation change.

Then. in your activity if you override onConfigurationChanged() method if you want to make any specific changes, which should occur on changing orientation:

@Override
public void onConfigurationChanged(Configuration config) {
  super.onConfigurationChanged(config);
}

There are many threads which can advice you how to deal with orientation change like:

Android orientation change calls onCreate

Activity restart on rotation Android

Avoid restarting threads on orientation change

Hope this helps.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • Brilliant, thank you! However, the overriden `onConfigurationChanged()` you provided doesn't do anything so it can be left out. – Adrian Jandl Jul 14 '13 at 09:05
1

You can use OnCreate() from Application class to do your file loading. This method executes only once.

[code samples]

MyApplication.java

package com.example.sunday;

import java.util.ArrayList;
import android.app.Application;

public class MyApplication extends Application {

    public ArrayList<Entry> listOfEntries;

    @Override
    public void onCreate() {
        super.onCreate();
        readFromFile(savedFile);
    }
    //define your readFromFile(File f) somewhere here
}

MainActivity.java

package com.example.sunday;

import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

    ArrayList<Entry> listOfEntries;

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

        MyApplication a = (MyApplication) getApplication();
        listOfEntries = a.listOfEntries;
    }
}

in AndroidManifest.xml you have to "register" your MyApplication class:

<application android:name=".MyApplication"
...
...
Vlad K.
  • 320
  • 3
  • 19
0

I believe the simplest solution is:

private TableLayout tblLayout;
private ArrayList<Entry> listOfEntries;
private boolean isFileLoaded = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    tblLayout = new TableLayout(this);

    if(!isFileLoaded){
        listOfEntries = readFromFile(saveFile);
        isFileLoaded = true;
    }

    setContentView(tblLayout);
}

but I will still use Application class as in my first answer - it gives me opportunity to "hide" readFromFile method in a different java file, making my Activity.java file cleaner.

Community
  • 1
  • 1
Vlad K.
  • 320
  • 3
  • 19