如何执行asynctask并同时启动按钮单击活动

时间:2015-11-30 05:11:19

标签: android android-asynctask

public void onClick(View v) {
    get_Brand = spBrand.getSelectedItem().toString();
    get_Model = spModel.getSelectedItem().toString();
    get_Category = spCategory.getSelectedItem().toString();
    Intent intentToProductList = new Intent(ActivityApplicationChart.this, ActivityAppChartToCategoryList.class);
    intentToProductList.putExtra("Brand_Name", get_Brand);
    intentToProductList.putExtra("Model_Name", get_Model);
    intentToProductList.putExtra("Category_Name", get_Category);
    startActivity(intentToProductList);
}

我在这里提供了按钮点击的代码。只要我点击按钮,新的Activity就会启动,并且还会执行一个asynctask。它将如何运作?请帮帮我专家:)

4 个答案:

答案 0 :(得分:0)

在单击按钮上执行asynctask并在asynctask的onPost方法中编写意图代码。

答案 1 :(得分:0)

假设您正在从活动A移动到B并且您在活动A处开始asynctask T,然后assectask T将继续在后台运行,而活动B可见。现在,当您按下活动B的后退按钮时,将执行onnctask T的onPostexecute,并且活动A将具有新的数据集或更新的视图。

答案 2 :(得分:0)

首先在按钮单击中,您可以在doInBackground方法中执行调用Web服务的异步任务,而不是在OnPostExecute方法中调用Intent之后执行 因此,在完成后台任务(WebService)后,您的postExecute任务调用(Intent)会自动完成。

答案 3 :(得分:0)

这是样本逻辑!您可以在致电AsyncTask之前执行startActivity(intentToProductList);, 或者你可以调用方法来启动AsyncTask的抽象方法onPreExecute中的活动。