等待片段添加到UI [英] Wait until fragment has been added to the UI

查看:91
本文介绍了等待片段添加到UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我达到了一个点,在横向模式下,我需要附加两个片段.为此,第二个片段需要等到第一个片段被附加(添加)后再添加.原因是第一个片段需要执行第二个片段所需的功能.我设法通过线程执行此操作,但是在这种情况下,它仅等待指定的时间,然后才附加第二个片段,并且如果在给定的时间内未附加第一个片段,则应用将崩溃,因为第二个片段没有必要的数据.

In my app I got to a point where, in landscape mode, I need to attach two fragments. In order to do that, the second fragment needs to wait until the first fragment has been attached (added), before it's being added. The reason for it is that the first fragment needs to execute a function that the second fragment needs. I managed to do that via a thread, but in this case it only waits the amount of time indicated, before attaching the second fragment, and in case the first fragment isn't attached in the time given, the app will crush because the second fragment doesn't have the necessary data.

还有什么更好的做法(示例),然后提供下面的代码(例如,等到附加第一个片段,并且在特定时间间隔内否)? :

Any better practices (examples) then the code below (like waiting until the first fragment is attached, and no on a certain time interval) ? :

getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.mainContent, fragment).commit();
        Thread thread = new Thread() {
            @Override
            public void run() {
                try {
                    synchronized (this) {
                        wait(1000);
                    }
                } catch (InterruptedException e) {

                }
                if (isLandscape) {
                openSecondFragment(mIndex, R.id.rightConent);
                }
            }
        };
        thread.start();

非常感谢.

我需要在第一个片段中执行处理程序:

The handler i need to be executed in the first fragment:

@SuppressLint("HandlerLeak")
protected void loadAccountList() {

    LoadAccountList loadAccountListThread = new LoadAccountList(new Handler() {

        @SuppressWarnings("unchecked")
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case LOAD_SUCCESSFUL_CODE:
                items = (ArrayList<Account>) msg.obj;
                break;
            case LOAD_FAILED_IOEXCEPTION_CODE:
                getActivity().showDialog(ERROR_DIALOG);
                break;
            default:
                break;
            }
        }
    });
    loadAccountListThread.start();

推荐答案

片段生命周期适用于您.根据文档,onStart()方法是当用户可见该片段时调用",因此我建议您在第一个片段类中执行以下操作:

Fragments lifecycle works in your favor here. According to the documentation the method onStart() is "Called when the Fragment is visible to the user", so I suggest you do something like this in your first fragment class:

public void onStart() {

    super.onStart();
    ((MainActivity)getActivity()).loadSecondFragment();
}

在您的活动中:

public void loadSecondFragment() {
    if (isLandscape) {
        openSecondFragment(mIndex, R.id.rightConent);
    }    
}

瞧瞧!尝试任何一种生命周期方法,看看哪种方法最适合您的目的.

And voilá! Try any of the lifecycle methods and see which one works best for your purpose.

这篇关于等待片段添加到UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆