I believe the Template method pattern involves encapsulating each step in the algorithm.
I think activity's life cycles (onCreate, onResume, etc) are steps that must be overridden by the concrete class.
Does this mean that the Android activity lifecycle (activity and fragment classes) conform to the template design pattern or is there a different pattern which suits it better
Thanks
Activity-lifecycle concepts To navigate transitions between stages of the activity lifecycle, the Activity class provides a core set of six callbacks: onCreate() , onStart() , onResume() , onPause() , onStop() , and onDestroy() . The system invokes each of these callbacks as an activity enters a new state.
onCreate() The Android oncreate() method is called at the very start when an activity is created. An activity is created as soon as an application is opened. This method is used in order to create an Activity.
There are seven methods that manage the life cycle of an Android application: onCreate()
OnStop is called when FirstActivity calls SecondActivity and FirstActivity looses visibility. If Second Activity has a transparent background then the FirstActivity will be visible underneath, so not loosing visibility and onStop on FirstActivity will never be called.
The way Android framework is built is definitely following the template pattern, which is its strength but also its weakness. Because this pattern suggests to implement only some part of the module, it gets pretty easy to obtain a quick and simple result without involving too many efforts.
However, as its is based on inheritance, this can get really nasty once you start thinking about extending the framework, or deal with cross-concerns pattern. Most of android framework require to extend an Activity in order to be used, and as multi-inheritance is not an option, this limits the way you can compose your features.
An approach that favors composition over inheritance would have been more than welcome, and the only reason I can imagine why this choice has been made is performance problems.
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