【问题标题】:AndroidAnnotations bean initialize when OnActivityResult called调用 OnActivityResult 时 AndroidAnnotations bean 初始化
【发布时间】:2015-02-21 10:07:52
【问题描述】:

编辑

这个问题是由于 ViewPager+TabInfo UI 结构,在执行AddTab 时重新实例化了子片段。我相应地发布了回复。

下面是原帖

在下面的代码中,bean injectedListAdapter 未初始化(null),但 @AfterInject@AfterView 在调用 @OnActivityResult 时也未执行。

为此,
1.如何让OnActivityResult@Bean注入后调用?
2. 还有,我可以在@AfterViews之后执行OnActivityResult吗?

@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.
    }
}

【问题讨论】:

  • 我不太明白这个问题。当从MainListFragment 开始的活动返回时,将调用onResult 方法。这些注入发生在初始化时。这些是独立的事件,但通常应该首先初始化,因为您应该首先在 init 之后启动 Activity

标签: java android onactivityresult android-annotations


【解决方案1】:

对不起@WonderCsabo

我的问题与 TabInfo+ViewPager UI 结构及其子片段有关。

下面的代码很脏但是可以工作。

在适配器中,

    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();
}

和包含ViewPager的Activity,每个tab按钮的索引值为Tag。因为我的应用只有静态标签页,所以做硬编码的工作就足够了。

@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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-20
    • 2017-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多