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

Fragment - wait for onCreateView() to complete before running methods

我正在努力应对与 Fragment 相关的一系列令人费解的事件。我试图将片段添加到 Activity,然后调用片段内的方法来更新一些文本。但是,我发现该方法正在片段 before 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 class:

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() 方法?怎么可能?

about to update title bar text
vertical title fragment is null
title fragment onCreateView()

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

在 fragmentTransaction.commit() 之后尝试 运行 fragmentManager.executePendingTransactions();在 titleBarFragment.updateTitleBar("Edit Log Entry", 20, false);

之前

定义一个侦听器接口并在您的 Activity 中实现它。

interface LyfecycleListener {
  void onCreatedView();
}

在你的Activity中:

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

只需在 activity

上使用 onStart()