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. 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?
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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.