Android Background Task Freezes UI Thread - Updating Firebase
<p>I have some code in onCreate() which is freezing the UI thread. I've tried putting it every different way I can find of to make it execute in the background. Only thing I can think of is that I have other firebase event listeners in the main UI thread, so even though the code is executing in the background, the other listeners are freezing the activity while the background updates. Or maybe my code is not really getting to the background?? Does anyone have any suggestions?</p>

<pre><code>  AsyncTask.execute(new Runnable() {
        @Override
        public void run() {
            //tried it here
    }
});
</code></pre>

<p>////////</p>

<pre><code>Thread thread = new Thread() {
        @Override
        public void run() {
            //tried it here
    }
 });

thread.start();
</code></pre>

<p>/////</p>

<pre><code>Runnable runnable = new Runnable() {
    @Override
    public void run() {
        //tried it here
    }
};
AsyncTask.execute(runnable);
</code></pre>

<p>/// here is the code. I am trying it this way at the moment. </p>

<pre><code> Thread custListLoadThread = new Thread(runnable);
    custListLoadThread.start();


}

private Runnable runnable = new Runnable() {
    @Override
    public void run() {

        mSelectedPlayerRef.child(mSelectedPlayer.getUsername()).setValue(mEmptyResultsItems);

        final ArrayList&lt;String&gt; mEnteredPlayers = new ArrayList&lt;&gt;();

        mEntriesRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                for(DataSnapshot entries: dataSnapshot.getChildren()){

                    Player player = entries.getValue(Player.class);
                    mEnteredPlayers.add(player.getUsername());

                    Player draftedPlayer = entries.getValue(Player.class);
                    mEntriesRef.child(draftedPlayer.getUsername()).child("VS")
                            .child(mSelectedPlayer.getUsername()).setValue(mEmptyResultsItems);

                }
                for(String enteredPlayer: mEnteredPlayers){



                    mSelectedPlayerRef.child(enteredPlayer).setValue(mEmptyResultsItems);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }

        });

    }
};
</code></pre>

<p>All of these are freezing the UI thread. </p>