1

我目前正在为 android 开发一种数据库应用程序。但是,由于条目数量很少,我觉得我不需要使用 Sqlite 数据库。现在,我目前从方法中的保存文件中“读取” onCreate()。但是,当我将手机从水平显示模式转到垂直显示模式时,这也会加载新文件,尽管它不会对性能产生很大影响,因为我没有那么多条目,但我仍然很困扰,因为我想以正确的方式去做。

这样做的“正确”方法是什么?

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);
}
4

3 回答 3

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.

于 2013-07-14T08:22:03.523 回答
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"
...
...
于 2013-07-14T08:22:26.827 回答
0

我相信最简单的解决方案是:

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);
}

但我仍然会像我的第一个答案一样使用 Application 类- 它让我有机会在不同的 java 文件中“隐藏” readFromFile 方法,使我的 Activity.java 文件更干净。

于 2013-07-14T09:46:38.823 回答