Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

android - Async Task in Fragment1 Blocks Another Async Task on different Fragment2

I have single activity App with a drawer menu

public class MainActivity extends ActionBarActivity {
....
void selectItem(int position) {

          Bundle args = new Bundle();
          args.putInt(PageFragment.ARG_Page_NUMBER, position);
          android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
          android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
          android.support.v4.app.Fragment fragment;
          int ActiveFragmentId = 3;
          if(position == 0 ){
             fragment = new Fragment1();
          } else if( position == 1){   
        fragment = new Fragment2();
    }
}
....

and the two fragments Fragment1 and Fragment2 both have async task in them

Now if the Asnc Task in Fragment1 task is started(i.e. hold a break point on onBackground() of the async task in Fragment1 ) and if i switch to Fragment2 the Async Task in Fragment2 will not work until the onPostExceute() of Fragment1 is executed. why is that?

Also getActivity() == null, inside onPostExecute() of Fragmnet1's Async Task, while switching to Fragment2

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As they mention in the Order of execution portion here, the order of execution depends on your API level.

You need to use task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params) instead of just task.execute(params).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...