android - What is the difference creating event callback or the activity itself within a fragment? -



android - What is the difference creating event callback or the activity itself within a fragment? -

lets using several fragments(action1fragment, action2fragment etc.) within activity(actionactivity). want access elements of activity object, or phone call methods of actionactivity. offered create event callback . if maintain reference actionactivity within action1fragment instead of keeping reference callbackinterface implemented actionactivity since using these fragments within particular activity.

i kinda confused thought activity might dead while reference of interface might still alive(it sounds ridiculous when read 1 time again ok if managed explain myself).

the android developer tutorials recommend utilize callback interface on fragments. activity hosts fragment must implement callback interface. fragment getactivity() , casts callback interface, , makes callback.

this recommended way promote more modular design. not matter if fragments ever work within 1 activity. if want create more generic fragments used different activities, above design pattern starts become useful. (for example: telephones fragment within person fragment , company fragment.)

suppose other way: fragment getactivity() , casts personactivity. fragment has access public methods of personactivity. design pattern becomes much more ugly when need other activity utilize fragment. fragment have changed first seek , cast personactivity, , if throws, seek companyactivity.

the recommended design pattern gives way create activity compatible fragment instead of vice versa. fragment knows callback interface , not of activities itself. activities know fragment because implement callback interface knew because constructed , initialized instance of it.

does create sense?

android android-fragments

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -