androidAhmed androidAhmed - 1 year ago 88
Android Question

use realm instance across different activities which using predefined realm database

I add predefined realm database to my app successufully and get the data


RealmConfiguration config1 = new RealmConfiguration.Builder(this)
.migration(new Migration())
realm = Realm.getInstance(config1);

private String copyBundledRealmFile(InputStream inputStream, String outFileName) {
try {
File file = new File(this.getFilesDir(), outFileName);
FileOutputStream outputStream = new FileOutputStream(file);
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = > 0) {
outputStream.write(buf, 0, bytesRead);
return file.getAbsolutePath();
} catch (IOException e) {
return null;

the problem how to get instance of the realm when I try using

realm = Realm.getDefaultInstance();

give me empty data
how can get instance across the app

Answer Source

To be able to get a default, you have to set a default:

RealmConfiguration config1 = new RealmConfiguration.Builder(this)
        .migration(new Migration())

Realm.setDefaultConfiguration(config1); // <-- here

Now you can get it with

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download