Last active
August 23, 2017 02:38
-
-
Save Zhuinden/2520d578ae4975cba84adeb8e1592d35 to your computer and use it in GitHub Desktop.
Realm Data Binding Example - MainActivity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity | |
extends RealmActivity { | |
Post post; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main); | |
post = realm.where(Post.class).equalTo("id", 1L).findFirst(); | |
post.addChangeListener(RealmDataBinding.FACTORY.create()); // this line is the key | |
binding.setPost(post); | |
binding.setActivity(this); | |
} | |
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
if(post != null) { | |
if(post.isValid()) { | |
post.removeChangeListeners(); // remove change listeners just in case | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment