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

Android 片段-在运行方法之前,等待onCreateView()完成,android,android-fragments,android-lifecycle,Android,Android Fragments,Android Lifecycle,我正在与一系列与片段相关的令人费解的事件作斗争。我试图将一个片段添加到活动,然后调用片段中的一个方法来更新一些文本。但是,我发现在onCreateView()完成之前,该方法正在片段中处理,这给我留下了一个空的视图对象,我的方法失败了。以下是活动代码: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R

我正在与一系列与
片段相关的令人费解的事件作斗争。我试图将一个片段添加到
活动
,然后调用片段中的一个方法来更新一些文本。但是,我发现在
onCreateView()
完成之前,该方法正在片段中处理,这给我留下了一个空的
视图
对象,我的方法失败了。以下是
活动
代码:

@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()
运行?谢谢。

尝试在fragmentTransaction.commit()之后运行fragmentManager.executePendingTransactions();在titleBarFragment.updateTitleBar之前(“编辑日志条目”,20,false)

定义侦听器接口并在活动中实现它

interface LyfecycleListener {
  void onCreatedView();
}
在您的活动中:

@Override
protected void onCreate(Bundle savedInstanceState) {
  ...
  this.titleBarFragment = new TitleBarVerticalFragment();
  this.titleBarFragment.setListener(this)
  ...
}

@Override
public void onCreatedView() {
  titleBarFragment.updateTitleBar("Edit Log Entry", 20, false);
}
在你的片段中:

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

...

  this.listener.onCreatedView();
}

只需在您的活动中使用
onStart()
,我通常在
片段中使用
构造函数
将值传递到
片段
。谢谢。我也可以使用setArguments。这个方法已经存在于fragment类中,所以我希望可以按原样重用它,而不是编辑类本身。我还有其他活动已经使用了这个片段,所以我不想也改变它们。我只是好奇为什么为了了解更多关于片段生命周期的信息,这篇文章不符合顺序。嗨@Alex,你能发布你的解决方案吗?什么对你有用?这是个好主意。然而,由于某种原因,它不起作用。谢谢你的建议。如果我理解正确的话,代码似乎是在对事件进行自动计时,而不是依赖Android来完成。是吗?是的,这是解决这类问题的一般方法,叫做。