setText 方法在 onViewCreated 方法中不起作用

发布于 2025-01-17 03:47:06 字数 4167 浏览 1 评论 0 原文

详细信息片段 -> onCreateView

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (fragmentBinding == null)
        fragmentBinding = FragmentDetails.inflate(inflater, container, false);
    return fragmentBinding.getRoot();
}

DetailsFragment -> onViewCreated

@Override
public void onViewCreated(@NonNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    int x = new Random().nextInt(10000);
    Log.w("Result", "... " + x);
    fragmentBinding.titleEditText.setText(String.valueOf(x));

}

当第一次打开 DetailsFragment 时,titleEditText 将从 x 变量中获取生成的数字(假设它 >528)

DetailsFragment 内部有一个按钮,可以将我导航到 BFragment 片段。

当导航到 BFragment 片段并执行某些任务时,我将按后退按钮返回 DetailsFragment

我期望的结果是什么?

BFragment 返回到 DetailsFragmentx 变量将采用一个新的随机数(假设它是 362)并放入它在里面titleEditText

当前结果是什么?

titleEditText 中的文本仍然具有 528 值,但必须采用 362

注释

  • 如果我将代码放在 onResume 方法中,效果会很好。
  • 我正在使用带有 backstack 的导航组件库。

改进了问题(添加了更多详细信息)

我构建了一个带有 2 个片段的简单应用程序来向您展示我的问题

通过 视频先生 Cheticamp,如果视频无法播放请告诉我,我会将其上传到 Youtube。

这是第一次运行应用时的Logcat

在此处输入图像描述

这是从 SecondFragment 返回到 FirstFragment 时的 Logcat

在此处输入图像描述

这是 FirstFragment 的代码

public class FirstFragment extends Fragment {

    private FragmentFirstBinding fragmentFirstBinding;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.w("FirstFragment", "onCreate");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.w("FirstFragment", "onCreateView");
        if (fragmentFirstBinding == null)
            fragmentFirstBinding = FragmentFirstBinding.inflate(inflater, container, false);
        return fragmentFirstBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.w("FirstFragment", "onViewCreated");

        fragmentFirstBinding.btnFirst.setOnClickListener(view1 -> Navigation.findNavController(view1).navigate(FirstFragmentDirections.actionFirstFragmentToSecondFragment()));

        int x = new Random().nextInt(10000);
        fragmentFirstBinding.edittext.setText(String.valueOf(x));
        Log.w("FirstFragment - X Value", "... " + x);
    }

}

这是 SecondFragment 的代码

public class SecondFragment extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_second, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        view.findViewById(R.id.btnSecond).setOnClickListener(view1 -> Navigation.findNavController(view1).popBackStack());
    }

}

DetailsFragment -> onCreateView

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (fragmentBinding == null)
        fragmentBinding = FragmentDetails.inflate(inflater, container, false);
    return fragmentBinding.getRoot();
}

DetailsFragment -> onViewCreated

@Override
public void onViewCreated(@NonNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    int x = new Random().nextInt(10000);
    Log.w("Result", "... " + x);
    fragmentBinding.titleEditText.setText(String.valueOf(x));

}

When open DetailsFragment for the first time, titleEditText will take the generated number from the x variable (Let's suppose it 528)

Inside DetailsFragment there is a button that will navigate me to the BFragment fragment.

When navigating to the BFragment fragment and doing some tasks, I will press the back button for back to DetailsFragment

What do I expect the result?

When back from BFragment to DetailsFragment the x variable will take a new random number (Let's suppose it 362) and put it inside titleEditText.

What is the current result?

The text inside titleEditText still have 528 value but it must take 362 value

Notes

  • It's working well if I put the code inside the onResume method.
  • I'm using the navigation component library with backstack.

Improved the question (Added more details)

I built a simple app with 2 fragments to show you my problem

Watch the result via the video on streamable.com, Mr
Cheticamp
, If the video doesn't work tell me and I'll upload it to Youtube.

Here is the Logcat when running the app for the first time

enter image description here

And this is the Logcat when back from SecondFragment to FirstFragment

enter image description here

Here is the code for FirstFragment

public class FirstFragment extends Fragment {

    private FragmentFirstBinding fragmentFirstBinding;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.w("FirstFragment", "onCreate");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.w("FirstFragment", "onCreateView");
        if (fragmentFirstBinding == null)
            fragmentFirstBinding = FragmentFirstBinding.inflate(inflater, container, false);
        return fragmentFirstBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.w("FirstFragment", "onViewCreated");

        fragmentFirstBinding.btnFirst.setOnClickListener(view1 -> Navigation.findNavController(view1).navigate(FirstFragmentDirections.actionFirstFragmentToSecondFragment()));

        int x = new Random().nextInt(10000);
        fragmentFirstBinding.edittext.setText(String.valueOf(x));
        Log.w("FirstFragment - X Value", "... " + x);
    }

}

And here is the code for SecondFragment

public class SecondFragment extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_second, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        view.findViewById(R.id.btnSecond).setOnClickListener(view1 -> Navigation.findNavController(view1).popBackStack());
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文