如何在切换片段时停止 AsyncTask?
Posted
技术标签:
【中文标题】如何在切换片段时停止 AsyncTask?【英文标题】:How to stop the AsyncTask while switching Fragments? 【发布时间】:2020-03-10 05:32:45 【问题描述】:我正在使用底部导航栏,Asynctask 在切换片段时不工作,需要等待 8 到 10 分钟才能从 asynctask 获得响应。由于 Asynctask 问题,所有页面都没有数据可显示。 请帮我解决这个问题。谢谢 片段切换代码:
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);
【问题讨论】:
请将您的代码添加到相关问题中 【参考方案1】:在您的片段中使用它,它将停止 AsyncTask...
@Override
protected void onDestroy()
super.onDestroy();
// cancel running task(s) to avoid memory leaks
if (runningTask != null) runningTask.cancel(true);
【讨论】:
【参考方案2】:使用并行执行。
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
【讨论】:
以上是关于如何在切换片段时停止 AsyncTask?的主要内容,如果未能解决你的问题,请参考以下文章
当用户切换到 Android Studio Kotlin 中的其他页面时,如何停止播放音乐?
自定义对话框片段内的进度条 - 如何从 AsyncTask 传递进度?