Search code examples
javaandroidonactivityresultandroid-annotations

AndroidAnnotations bean initialize when OnActivityResult called


EDIT

This problem is due to ViewPager+TabInfo UI structure, which child fragments are re-instanciated when AddTab performed. I post a reply accordingly.

ORIGINAL POST BELOW

In below code, the bean injectedListAdapter is not initialized(null) but also @AfterInject and @AfterView are not executed when @OnActivityResult is invoked.

For that,
1. How can I make OnActivityResult called after @Bean injected?
2. And, can I make to execute OnActivityResult after @AfterViews?

@EFragment(R.layout.fragment_list_main)
public class MainListFragment extends Fragment {
    @Bean
    InjectedListAdapter injectedListAdapter;

    @AfterInject
    void initValues() {
        // ...
    }

    @AfterViews
    void initViews() {
        // ...
    }

    @OnActivityResult(CommonValue.REQUEST_CODE_A)
    void onResult(int resultCode, Intent data) {
        injectedListAdapter.doSomething(); // <- injectedListAdapter is null.
    }
}

Solution

  • Sorry @WonderCsabo

    My problem is related to TabInfo+ViewPager UI structure and its children fragments.

    below code is darn dirty but works.

    In adapter,

        private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
    
        public void addTab(ImageButton btn, Class<?> clss, Bundle args) {
        TabInfo info = null;
        if (mContext.getSupportFragmentManager().getFragments() != null && mContext.getSupportFragmentManager().getFragments().size() > 0) {
            for (Fragment fragment : mContext.getSupportFragmentManager().getFragments()) {
                if((int)btn.getTag() == 0 && fragment instanceof Fragment1_){
                    info = new TabInfo(fragment.getClass(), fragment, btn, args);
                    continue;
                }
                if((int)btn.getTag() == 1 && fragment instanceof Fragment2_){
                    info = new TabInfo(fragment.getClass(), fragment, btn, args);
                    continue;
                }
                if((int)btn.getTag() == 2 && fragment instanceof Fragment3_)
                {
                    info = new TabInfo(fragment.getClass(), fragment, btn, args);
                    continue;
                }
            }
        }
    
        if(info == null) {
            info = new TabInfo(clss, null, btn, args);
        }
    
        btn.setOnClickListener(this);
        mTabs.add(info);
        notifyDataSetChanged();
    }
    

    And Activity which contains ViewPager, each tab button has index value of Tag. As my app only has static tab pages, it is enough to do hard-coded work.

    @AfterViews
    void initViews() { // you can put it in OnCreate()
            imageButtonList.setTag(0);
        imageButtonGroup.setTag(1);
        imageButtonProfile.setTag(2);
        mMainPagerAdapter.addTab(imageButton1, Fragment1_.class, null);
        mMainPagerAdapter.addTab(imageButton2, Fragment2_.class, null);
        mMainPagerAdapter.addTab(imageButton3, Fragment3_.class, null);
    }