I’m learning android but have a pretty fair background in java so am a little bit confused by a lot of the tutorials extending (sub-classing) Activity directly ?
I want to handle the activity lifecycle correctly in all of my activities, I have about 4 maybe 5 but obviously each activity could have the home key pressed, the user could just hit the power button.
What i’d like is a single Activity that implements the various stages in the lifecycle in the context of my application
e.g
public class MyActivity extends Activity {
//overide the lifecycle methods
protected void onCreate(Bundle savedInstanceState) { }
protected void onStart() {
//do my start stuff
}
protected void onResume() {
//open db connection
}
protected void onPause() {
//close db connection
}
}
I’m asking because I’m used to working with databases and it seems strange to handle closing the connection in each activity when I could handle it at a higher level.
I’d then like to extend MyActivity is this a reasonable approach or am I way off the mark here ?
I think its a good idea to have an
Abstractclass which ExtendsActivityand put all the common operations (like Home button operations you mentioned, progress bar operations, search functionality etc.,), constants used by all yourActivities. You might also want to subclassApplicationand store variables which needs to be accessed in a global context.Take a look at some of the applications that Google develops and try to use those design patterns. I/O Schedule app is one of them.