Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the "android activity lifecycle" use the Template Method pattern?

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

like image 565
ShakeJ Avatar asked Sep 16 '14 01:09

ShakeJ


People also ask

What is Android activity life cycle method?

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.

Which method is used in Android to create activity?

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.

How many methods are there in Android activity lifecycle?

There are seven methods that manage the life cycle of an Android application: onCreate()

When onStop method is called in Android?

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.


1 Answers

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.

like image 146
Francis Toth Avatar answered Oct 09 '22 03:10

Francis Toth