Search code examples
javaandroidsqliteandroid-fragmentsandroid-contentresolver

Database operations on activity or fragment?


I have a ContentResolver for all my database operation. The MainActivity contains one fragment and the navigation drawer. This fragment diplay data from database and I need to make some operations on this date (like update, delete...).

My question is, where is the best place to make this operations ? Directly in the fragment with getActivity().getContentResolver() ? Or with a callback to the activity and put all operation in the activity ?


Solution

  • While this is much an opinion based question, since most people will have different tastes and numerous different ways to implement it, here is my two cents:

    It wouldn't do you any favor to put them in the activity and provide callbacks. If the database operations are inseparable from the fragment, it is ok to do these operations within the fragment. Because you would be able to easily reuse the fragment in another activity, without implementing the callbacks for each activity again and again.