Skip to content
This repository has been archived by the owner on Feb 26, 2023. It is now read-only.

Fragment activity communication

PerfectCarl edited this page Jan 13, 2014 · 10 revisions

Since AndroidAnnotations 3.1

Introduction

The @Shared and @Share annotations ease the sharing of complex objects between a Fragment and its container Activity.

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 fragment have access to objects that live within the activity lifecycle like SQLiteOpenHelper.

Sample

The parent activity:

@EActivity(R.layout.activity_main)
public class MainActivity extends Activity {

	@Share
	String param1 = "activity string";

	@Share
	Bundle param2;

	@Share
	@Bean
	MyBean bean;

	@Share
	int someInt = 42;

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

And the fragment

@EFragment(R.layout.fragment_main)
public class MainFragment extends Fragment {

	@Shared
	String param1 = "fragment string";

	@Shared
	Bundle param2;

	@Shared
	MyBean bean;

	@Shared
        String isNotSharedByActivity ; 

	@Shared
	int someInt = 42;

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

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 shared immutable objects ('String, 'Integer`...) will be successfully initialized with the parent activity values but any subsequent modifications won't be passed on.

Memory management and fragment recycling

Sharing object instances between objects with [different lifecycles] (http://developer.android.com/guide/components/fragments.html#Creating) may create memory leaks. Also fragments can be reused once they have been removed from the current layout.

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.

Using AndroidAnnotations

Questions?

Enjoying AndroidAnnotations

Improving AndroidAnnotations

Extending AndroidAnnotations

Clone this wiki locally