Fragment activity communication

Csaba Kozák edited this page May 3, 2015 · 10 revisions

This is a planned feature, does not exist yet

Since AndroidAnnotations X.Y


The @Shared and @Share annotations enable an Activity to share complex objects with its Fragment.

The activity annotates with @Share the members that will be exposed to its children fragments. The matching member of the fragment that is annotated with @Shared will then reference the activity shared member.

Those annotations let fragments have access to objects that live within the activity lifecycle like SQLiteOpenHelper.


The parent activity:

public class MainActivity extends Activity {

	String param1 = "activity string";

	Bundle param2;

	MyBean bean;

	int someInt = 42;

	void initInject() {
		// You can do custom member initialization here
		param2 = new Bundle();
		param2.putString("are we", "done?");

And the fragment

public class MainFragment extends Fragment {

	String param1 = "fragment string";

	Bundle param2;

	MyBean bean;

	String isNotSharedByActivity ; 

	int someInt = 42;

	void init() {
		// All the members are initialized and ready !

Shared members assignment

The fragment shared members must have the exact same name (and assignable types, of course) as the ones that the activity shares.

The fragment shared members are initialized just before the @AfterViews callback. They won't be initialized in time for the @AfterInject callback.

If the parent activity doesn't share any member, the fragment shared members are left untouched.

If the parent activity shares members, then the fragment shared members will be resolved and initialized. Any member that doesn't match an activity shared member will be initialized with null and a warning will be displayed in the logcat console:

The member 'isNotSharedByActivity' is not declared by the activity 'MainActivity' or is not annotated with @Share

Primitive types and immutable objects

Shared member of primitive types (int, boolean...) and or immutable types (String, Integer...) will be successfully initialized with the parent activity values but any subsequent modifications won't be passed on (as you might have expected).

Memory management and fragment recycling

Fragments can be reused once they have been removed from the current layout. Also sharing object instances between objects with [different lifecycles] ( may create memory leaks.

For those reasons, all fragment shared members will be reset to null when the fragment is destroyed (onDestroyView() event), whether the parent activity shares members or not.

14/10/2018 The 4.5.1 release is out !

Using AndroidAnnotations


Enjoying AndroidAnnotations

Improving AndroidAnnotations

Extending AndroidAnnotations

Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.