Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

Sharing members between an activity and its fragment(s) #854

Copy link
Copy link
@PerfectCarl

Description

@PerfectCarl
Issue body actions

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

Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.