using asynctask in a fragment to load a database with progress bars
<p>hi sorry if this is beginner stuff but im really struggling to use asynctask in my fragments with a progress bar, particularly when rotating as im using nested fragments so i cant use setretaininstance(true), i tried following <a href="http://www.michenux.net/android-asynctask-in-fragment-best-pratices-725.html" rel="nofollow">this</a> quick tutorial which resulted in my approach below</p>

<pre><code>public class OneFragment extends fragment {
private WeakReference&lt;MyAsyncTask&gt; asyncTaskWeakRef;
}

private void startNewAsyncTask() {
    MyAsyncTask asyncTask = new MyAsyncTask(this);
    this.asyncTaskWeakRef = new WeakReference&lt;MyAsyncTask &gt;(asyncTask );
    asyncTask.execute();
}

public void showProgressBar() {
    ProgressBar progress = (ProgressBar)getActivity().findViewById(R.id.progressBarFetch);
    progress.setVisibility(View.VISIBLE);
    progress.setIndeterminate(true);
}

public void hideProgressBar() {
    ProgressBar progress = (ProgressBar)getActivity().findViewById(R.id.progressBarFetch);
    progress.setVisibility(View.GONE);

}

private class MyAsyncTask extends AsyncTask&lt;Void, Void, Void&gt; {

    private WeakReference&lt;OneFragment&gt; fragmentWeakRef;

    private MyAsyncTask (OneFragment fragment) {
        this.fragmentWeakRef = new WeakReference&lt;OneFragment&gt;(fragment);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        showProgressBar();
    }

    @Override
    protected Void doInBackground(Void... params) {
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(getActivity(), "ADD_NEW_CARD", null);
        SQLiteDatabase db = helper.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(db);
        DaoSession daoSession = daoMaster.newSession();
        addNewCardDao leaseDao = daoSession.getAddNewCardDao();
        QueryBuilder qb = leaseDao.queryBuilder();
        SharedPreferences sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(getActivity());
        //  Create a new boolean and preference and set it to true
        prefString = sharedPreferences.getString("ICONS SELECTED","ORIGINAL");
        switch (prefString){
            case "ORIGINAL":

                leaseList.clear();
                String[] strings1 = {"ORIGINAL", "USER"};
                qb.where(addNewCardDao.Properties.CardIconType.in(strings1),
                        qb.and(addNewCardDao.Properties.Type_of_type.eq("PEOPLE"),
                                addNewCardDao.Properties.Type_of_word.eq("NOUNS")));
                leaseList = qb.list();
                db.close();
                break;
            case "SIMPLE":
                leaseList.clear();
                String[] strings2 = {"SIMPLE", "USER"};
                qb.where(addNewCardDao.Properties.CardIconType.in(strings2),
                        qb.and(addNewCardDao.Properties.Type_of_type.eq("PEOPLE"),
                                addNewCardDao.Properties.Type_of_word.eq("NOUNS")));
                leaseList = qb.list();
                db.close();
                break;
            case "PHOTOS":
                leaseList.clear();
                String[] strings3 = {"PHOTOS", "USER"};
                qb.where(addNewCardDao.Properties.CardIconType.in(strings3),
                        qb.and(addNewCardDao.Properties.Type_of_type.eq("PEOPLE"),
                                addNewCardDao.Properties.Type_of_word.eq("NOUNS")));
                leaseList = qb.list();
                db.close();
                break;
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void response) {
        super.onPostExecute(response);
        if (this.fragmentWeakRef.get() != null) {
            hideProgressBar();
            CardAdapterDB cardAdapterDB = new CardAdapterDB(leaseList);
            recyclerView.setAdapter(cardAdapterDB);
            cardAdapterDB.notifyDataSetChanged();
        }
    }
}
</code></pre>

<p>this seems to work but on rotation crashes with a null pointer error for the progress bar so i tried changing the progressbar find view getActivity() call to rootView because thats how i inflate my view </p>

<pre><code> View rootView = inflater.inflate(R.layout.fragment_one, container, false);
</code></pre>

<p>but this doesnt work it crashes again with a null pointer but this time before the app starts, but i definitely have this progressbar in my layout </p>

<pre><code>&lt;ProgressBar
android:id="@+id/progressBarFetch"
style="?android:attr/progressBarStyleLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true" /&gt;
</code></pre>

<p>and im only trying to reference it after the fragment is attached i think,
i have many fragments all needing this change all using the same layout all loading from the same database just using different queries, so is it better to make a static method for the async and progress bar, or should they be unique to each fragment, and how can i handle the orientation for nested fragments</p>

<p>can anyone point me in the right direction with this or maybe point out something im missing</p>