Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Android Realm – Initialize Database

Before you can use Realm in your application, you must initialize it. This only has to be done once using method Realm.init(context); You must provide an Android context. A good place to initialize Realm is in onCreate method of an application subclass. If you create your own application subclass, you must add it to the AndroidManifest.xml

AndroidManifest.xml


    ...
    
    ...

MyApplication.java

public class MyApplication extends Application {

 @Override
 public void onCreate() {
  super.onCreate();
  Realm.init(this); // Initialize Realm
 }

}

MyActivity.java

public class MyActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  Realm realm = Realm.getDefaultInstance(); // Get a Realm instance for this thread
  try {
   // ... Do something ...
  } finally {
   realm.close();
  }
 }

}


This post first appeared on Javac, please read the originial post: here

Share the post

Android Realm – Initialize Database

×

Subscribe to Javac

Get updates delivered right to your inbox!

Thank you for your subscription

×