当前位置:  开发笔记 > 编程语言 > 正文

片段 - 在运行方法之前等待onCreateView()完成

如何解决《片段-在运行方法之前等待onCreateView()完成》经验,为你挑选了0个好方法。

我正在努力解决与a有关的一系列令人费解的事件Fragment.我正在尝试向一个片段添加一个片段Activity,然后在片段内调用一个方法来更新一些文本.但是,我发现的是该方法 onCreateView()完成之前在片段中处理,这使我留下了一个空View对象,并且我的方法失败了.这是Activity代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_log_entry_details);

    fragmentManager = getSupportFragmentManager();

    titleBarFragment = new TitleBarVerticalFragment();

    fragmentTransaction = fragmentManager.beginTransaction ();
    fragmentTransaction.add(R.id.log_entry_title_frame, titleBarFragment);
    fragmentTransaction.commit();

    titleBarFragment.updateTitleBar("Edit Log Entry", 20, false);
}

看起来很简单.这是TitleBarVerticalFragment类:

public class TitleBarVerticalFragment extends TitleBarFragment {

    @Inject SharedVisualElements sharedVisualElements;

    View view;
    TextView titleLabel;

    public TitleBarVerticalFragment() {
        // add this line for any class that want to use any of the singleton objects
        Injector.INSTANCE.getAppComponent().inject(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        super.onCreateView(inflater, container, savedInstanceState);

        Log.d(Constants.TAG, "title fragment onCreateView()");

        view = inflater.inflate(R.layout.fragment_title_bar_vertical, container, false);

        ImageView logoImage = (ImageView) view.findViewById(R.id.logo_vertical);
        titleLabel = (TextView) view.findViewById(R.id.verticalTitleLabel);

        titleLabel.setTextColor(sharedVisualElements.secondaryFontColor());
        titleLabel.setTypeface(sharedVisualElements.font());
        titleLabel.setTextSize(20);

        logoImage.setImageDrawable(sharedVisualElements.logoImage());
        logoImage.setBackgroundColor(Color.TRANSPARENT);

        return view;
    }

    public void updateTitleBar(String text, int textSize, boolean titleLabelIsHidden) {

        Log.d(Constants.TAG, "about to update title bar text");

        if (view == null) {
            Log.d(Constants.TAG, "vertical title fragment is null");
            return;
        }

        if (titleLabel == null)
            titleLabel = (TextView) view.findViewById(R.id.verticalTitleLabel);

        if (titleLabel == null) {
            Log.d(Constants.TAG, "vertical title label is null");
            return;
        }

        Log.d(Constants.TAG, "updating title text: " + text);
        titleLabel.setText(text);
        titleLabel.setTextSize(textSize);
    }

请注意此logcat输出的顺序.请注意该方法onCreateView()似乎如何运行?怎么可能?updateTitleBar()

即将更新标题栏文本
垂直标题片段为空
标题片段onCreateView()

onCreateView()在调用任何片段的其他方法之前,如何确保运行?谢谢.

推荐阅读
mobiledu2402851173
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有