简体   繁体   English

只能加载片段视图一次

[英]Load fragment view only once

I've got the following. 我有以下几点。 I have an navigation drawer. 我有一个导航抽屉。 When I click on one item the main content frame gets replaced with another fragment dependent on what the user clicks on. 当我单击一项时,取决于用户单击的内容,主要内容框架将替换为另一片段。 The fragment then starts an asynctask to create the view. 然后,该片段启动asynctask来创建视图。 I do this so the app doesn't get stuck and the user can actually see that something is getting done. 我这样做是为了使应用程序不会卡住,用户实际上可以看到已经完成了某些事情。

The problem I'm encountering now is that, every time I click on the item the fragment gets loaded again. 我现在遇到的问题是,每次我单击该项目时,片段都会再次加载。 I know that this is because I'm calling my AsyncTask in the onCreate method of the fragment, but is there a work around on how to NOT load the whole thing again when it has been already loaded once? 我知道这是因为我正在片段的onCreate方法中调用AsyncTask,但是是否已经解决了如何在已经加载一次后不再重新加载整个事情呢?

Long story short: 长话短说:

I have a fragment that gets filled with data. 我有一个充满数据的片段。 I don't want the user to think that the app is stuck so I added a progress bar and an async task so the user can see that something's getting done. 我不希望用户认为应用程序卡住了,所以我添加了进度栏和异步任务,以便用户可以看到正在完成某件事。

Problem the app loads everything again when I click on one item Solution I want that when the fragment is loaded once it gets saved and not called again. 问题是,当我单击一项时,应用程序会再次加载所有内容。 解决方案我希望在片段被保存后立即加载而不再次调用。

This is here my code. 这是我的代码。 I minified it so you don't need to read too much. 我将其缩小,因此您不需要阅读太多。 Just used the fewest lines possible 只需使用最少的行

MAINFRAGMENT 主片段

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

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState){

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        StrictMode.setThreadPolicy(policy);
        View rootView = inflater.inflate(R.layout.main_fragment, container, false);
        rl = (RelativeLayout) rootView.findViewById(R.id.mainRelativeLayout);
        sv = (ScrollView) rootView.findViewById(R.id.mainScrollView);
        ll = (LinearLayout) rootView.findViewById(R.id.mainListView);
        progressBar = (ProgressBar) rootView.findViewById(R.id.progressBar1);
        return rootView;
    }

ASYNCTASK 异步任务

private class MainAsyncTask extends AsyncTask<Void, Void, Void> {

        TextView[] source;
@Override
        protected void onPreExecute() {
//          super.onPreExecute();
            progress = 0;
        }

        @Override
        protected Void doInBackground(Void... params) {
            source      = new TextView[todayHashMapSize];
for (int i = 0; i < todayHashMapSize; i++) {

                source[i]       = new TextView(getActivity());
}
return null
}

protected void publishProgress(int value) {
               progressBar.setProgress(value);
        }

        @Override
        protected void onPostExecute(Void result){
            for (int i = 0; i < todayHashMapSize; i++) {
                ll.addView(source[i]);


            }
            ll.removeView(progressBar);

        }

So basically is there an option to check whether an layout already has been populated and if so how do I do that, where do I put that code? 因此,基本上有一个选项可以检查是否已经填充了布局,如果可以的话,该怎么做?将代码放在哪里?

My approach was to check whether ll has childs with 我的方法是检查是否有孩子

if(ll.getChildCount() == 0){
        new MainAsyncTask().execute();
    }

but that just gives me an nullPointerException at the exact same line 但这只是在完全相同的行上提供了一个nullPointerException

Use a headless fragment with a call to 使用无头片段调用

setRetainInstance(true);

to prevent it being destroyed. 防止它被破坏。

See these links for more info: http://www.vogella.com/articles/AndroidBackgroundProcessing/article.html#fragmentsandbackgroundprocessing 有关更多信息,请参见以下链接: http : //www.vogella.com/articles/AndroidBackgroundProcessing/article.html#fragmentsandbackgroundprocessing

http://creeder.com/?&page=AsyncTask http://creeder.com/?&page=AsyncTask

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM