Hello,
as it is, the communication between Activity and Fragment classes is a little bit complicated.
Bundle and intents work only for simple data. But at the moment there is no easy way to pass "real objects" onto activities (like DbHelper, or image downloaders...).
What I propose a new annotation @shared
@EActivity(R.layout.activity_main)
public class MainActivity extends Activity {
@Shared
String param1 = "string from the activity";
@Shared
Bundle param2;
@Shared
@Bean
MyBean bean;
@AfterInject
void initInject() {
// You can do custom member initialisation here
param2 = new Bundle();
param2.putString("are we", "done?");
}
}
And
@EFragment(R.layout.fragment_main)
public class MainFragment extends Fragment {
@Shared
String param1 = "string from the fragment";
@Shared
Bundle param2;
@Shared
MyBean bean;
@AfterViews
void init() {
// All the members are initialized and ready !
}
}
I did a little mockup to validate the idea and it works nicely.
Here is the generated code
public final class MainFragment_ extends MainFragment ... {
@Override
public void onViewChanged(HasViews hasViews) {
...
initShared_();
...
}
private void initShared_() {
if (getActivity() instanceof SharingActivity_) {
SharingActivity_ act = (SharingActivity_) getActivity();
param1 = (String) act.getSharedObject_("param1");
param2 = (Bundle) act.getSharedObject_("param2");
bean = (MyBean) act.getSharedObject_("bean");
}
}
// Reset the shared members to prevent memory leaks
private void resetShared_() {
param1 = null ;
param2 = null ;
bean = null ;
}
public void onDestroyView () {
resetShared_() ;
super.onDestroyView() ;
}
The parent activity implements SharingActivity_ if it exposes shared members.
public interface SharingActivity_ {
Object getSharedObject_(String name);
}
public final class MainActivity_ extends MainActivity implements
... SharingActivity_ {
@Override
public Object getSharedObject_(String name) {
if ("param1".equals(name))
return param1;
if ("param2".equals(name))
return param2;
if ("bean".equals(name))
return bean;
Log.d( "TAG", "The member '+name +"' is not declared by the activity 'MainActivity' or is not annotated with @Share" ) ;
return null;
}
The getSharedObject_ method returns something only for shared members.
I think that along with Otto that could really ease the development with Fragment.
edit: reset shared variables to prevent memory leaks
Hello,
as it is, the communication between
ActivityandFragmentclasses is a little bit complicated.Bundle and intents work only for simple data. But at the moment there is no easy way to pass "real objects" onto activities (like DbHelper, or image downloaders...).
What I propose a new annotation @shared
And
I did a little mockup to validate the idea and it works nicely.
Here is the generated code
The parent activity implements
SharingActivity_if it exposes shared members.The
getSharedObject_method returns something only for shared members.I think that along with Otto that could really ease the development with
Fragment.edit: reset shared variables to prevent memory leaks