Understandable this question is a little subjective, but I believe there is a concrete answer.
I have a really small activity that does nothing more than display some data to the user in a ListActivity. The data I use comes from the app's SQLite database and I find myself doing this
public class MainActivity extends ListActivity{ private DatabaseAdapter dbHelper; @Override public void onCreate(Bundle b){ dbHelper = new DatabaseAdapter(this); super.onCreate(b); } @Override public void onResume(){ dbHelper.open(); fill_data();//a small method that uses dbHelper to setListAdapter super.onResume(); } @Override public void onPause(){ dbHelper.close(); super.onPause(); } private void fill_data(){/*makes use of dbHelper */ }
Is this just code largely unnecessary? I'd like to handle the lifecycles so I can use the least amount of resources when MainActivity is not on top, but at the same time, this whole activity could be done in onCreate()
.
You could probably get away with not overriding the onPause method if you just open and close the dbHelper whenever you use it. It's not all that intensive to open the helper (I'm assuming SQLiteOpenHelper), so you might consider it premature optimization.
At any rate, you'll almost always need to override onCreate, often override onResume, and sometimes override onPause. What's important is when the framework calls the various callbacks and what you have to do in them.
If you need to do something when your Activity hits the foreground or if you want to do something in your Activity's Looper (say, location updates), you'll have to override onResume. If you need to clean up after yourself, you'll have to implement onPause.
I generally don't implement onDestroy because it is not guaranteed to be called.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With