构建片段后是否可以调用方法?

时间:2019-04-30 08:24:33

标签: java android

我想更改一个Fregment,并且在Gui建成后就应该执行一个方法。

到目前为止,我已经用线程解决了它。只有我认为必须有更好的方法。 在形成片段之前执行OnResume()。 没有人知道没有线程解决的方法吗?

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    fragmentRoot = inflater.inflate(R.layout.fragment_l2c_printersettings, container, false);
    fragmentRoot.setFocusableInTouchMode(true);
    fragmentRoot.requestFocus();

    ...
    ...
    ...

    new Thread(new Runnable() {
        public void run() {
            try {
                Thread.sleep(1000);
                loadPrinter(); 
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }).start();

    return fragmentRoot;
}

2 个答案:

答案 0 :(得分:0)

我认为您可能正在片段中寻找public void onActivityCreated(Bundle savedInstanceState)

片段生命周期。

enter image description here

From the documentation

  

在创建片段的活动并实例化此片段的视图层次结构时调用。一旦这些部分就位,就可以用来进行最终初始化,例如检索视图或还原状态。这对于使用setRetainInstance(boolean)保留其实例的片段也很有用,因为此回调告诉片段何时将其与新的活动实例完全关联。在onCreateView(LayoutInflater,ViewGroup,Bundle)之后和onViewStateRestored(Bundle)之前调用。

答案 1 :(得分:0)

您可以在onViewCreated中调用该函数。到调用onViewCreated时,该片段已完全加载。

相关问题