如何在切换碎片时停止AsyncTask



我正在使用底部导航栏,切换片段时异步任务不工作,需要等待8到10分钟才能从异步任务获得响应。由于异步任务问题,所有页面都没有可显示的数据。请帮我解决这个问题。谢谢片段切换代码:

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected( MenuItem item) {
Fragment fragment=null;
switch (item.getItemId()) {
case R.id.navigation_home:
fragment = new HomeFragment();
break;
case R.id.navigation_market:
fragment=new MarketFragment();
break;
case R.id.navigation_agent:
fragment=new AgentFragment();
break;
case R.id.navigation_finance:
fragment=new FinanceFragment();
break;
/* case R.id.navigation_more:
MoreFragment moreFragment=new MoreFragment();
moreFragment.show(getSupportFragmentManager(),moreFragment.getTag());*/
}
closeContextMenu();
return loadFragment(fragment);
}
};
private boolean loadFragment(Fragment fragment) {
if (fragment != null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, fragment)
.commit();
return true;
}
return false;
}```

异步任务代码:我在所有片段中都实现了这样的异步任务。

JSONObject jsonObject, jsonObject2;
@Override
protected void onPreExecute() {
super.onPreExecute();
SignupActivity.this.layoutProgress.setVisibility(View.VISIBLE);
}
@Override
protected String doInBackground(Void... voids) {
HashMap<String, String> map = new HashMap<>();
map.put("countrycode",code);
map.put("phonenumber",phone);
map.put("emailid",email);
map.put("agentname",fullname);
map.put("username",username);
map.put("password",lpassword);
map.put("confirmpassword",conlpassword);
map.put("transactionpassword",spassword);
map.put("transactionconfirmpassword",conspassword);
return new HttpHandler().sendPostRequest(Config.url + "JoinNowRegister",map);
}
@Override
protected void onPostExecute(String response) {
super.onPostExecute(response);
Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_LONG).show();
try {
jsonObject = new JSONObject(response);
if(jsonObject.getString("statuscode").equals("1")){
message = jsonObject.getString("status");
jsonObject2 = jsonObject.getJSONObject("data");
id = jsonObject2.getString("agentid");
int code=Integer.parseInt(jsonObject2.getString("countrycode"));
String phonenumber= jsonObject2.getString("phone");
storeCode(code);
storePhone(phonenumber);
Intent intent;
intent = new Intent(SignupActivity.this, OtpActivity.class);
Bundle bundle = new Bundle();
bundle.putString("agentid",id);
intent.putExtras(bundle);
startActivity ( intent );
finish();
}
else{
msg=jsonObject.getString("data");
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
SignupActivity.this.layoutProgress.setVisibility(View.GONE);
}
}   

在片段中使用它,它将停止AsyncTask。。。

@Override
protected void onDestroy() {
super.onDestroy();
// cancel running task(s) to avoid memory leaks
if (runningTask != null) runningTask.cancel(true);
}

使用并行执行。

task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

相关内容

  • 没有找到相关文章

最新更新