Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ORMLite with Fragments

Is it possible to use ORMLite to manage your local database while using Android Fragments?

Some example code or links to example code showing how to create something like an ORMLiteFragmentActivity class would be cool. Or I guess a simple "no" might be an acceptable answer. :)

like image 676
wuliwong Avatar asked Dec 08 '22 20:12

wuliwong


1 Answers

Yup, it's totally possible. See the instructions here. Basically, all you do is create a new class, like you suggested. You can replace Fragment with any other type that you want to extend. For example, I also have OrmLiteListFragment which extends ListFragment

public class OrmLiteFragment extends Fragment {

    private DatabaseHelper databaseHelper = null;

    protected DatabaseHelper getHelper() {
        if (databaseHelper == null) {
            databaseHelper =
                OpenHelperManager.getHelper(getActivity(), DatabaseHelper.class);
        }
        return databaseHelper;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (databaseHelper != null) {
            OpenHelperManager.releaseHelper();
            databaseHelper = null;
        }
    }
}
like image 82
compuguru Avatar answered Dec 28 '22 22:12

compuguru