0

案例: 我正在使用Async Task顯示某些列表。當我點擊listview的任何項目時,另一個活動就會出現上一個活動的前景。該前景活動包含前景活動完成的完成按鈕。禁用重新執行異步任務完成前臺活動

問題: 當我按下Finish按鈕前景活動結束,但具有異步任務,去年的活動再次執行。

需要: 我想當我按完成按鈕,然後只有前景活動完成和具有異步任務的後臺活動不能再執行。該背景活動必須與打開前臺活動之前的活動相同。

編輯:具有非同步任務 頭等艙

protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.restaurant_detail); 
    Intent i = getIntent(); 
    res_id = i.getStringExtra("id"); 
    gps = new GPSTracker(getApplicationContext()); 
    session = new SessionManagement(getApplicationContext()); 
    fillDatabase = new DatabaseHandler(getApplicationContext()); 
    fillDatabase.open(); 
    count = fillDatabase.fetchCount(); 
    SetUpViews(); 
    SimpleDateFormat sdf = new SimpleDateFormat("EEEE"); 
    Date d = new Date(); 
    dayOfTheWeek = sdf.format(d); 
    Log.i("WEEKDAY", dayOfTheWeek); 
    imageLoader = new ImageLoader(getParent()); 
    progressDialog = ProgressDialog.show(getParent(), "", "Loading..."); 
    Log.v(String.valueOf(count), "count"); 
    // new MenuTask().execute(); 
    new DetailsTask().execute(); 


} 
public class DetailsTask extends AsyncTask<String, String, String> { 

    @Override 
    protected String doInBackground(String... arg0) { 
     // Creating JSON Parser instance////////// 
     JSONParser jParser = new JSONParser(); 
     // Getting JSON String from URL.............. 
     JSONObject jObject = jParser 
       .getJSONFromUrl("http://smartaway.dk/json/rest_get_details.php?id=" 
         + res_id); 
     try { 
      // /Getting array of Events....... 
      details = jObject.getJSONArray(TAG_NODE); 
      ///LOOping through AllEvents........ 
      for (int i = 0; i < details.length(); i++) { 
       JSONObject e = details.getJSONObject(i); 
       res_id = e.getString(TAG_ID); 
       res_name = e.getString(TAG_RESNAME); 
       logo = e.getString(TAG_RES_LOGO); 
       name = e.getString(TAG_RESNAME); 
       city_state = e.getString(TAG_CITY); 
       street = e.getString(TAG_STREET); 
       localityString = e.getString(TAG_LOCALITY); 
       restype = e.getString(TAG_RESTYPE); 
       Log.v(restype, "restype"); 
       lat = e.getString(TAG_LAT); 
       Log.v(lat, "latitude"); 

       lng = e.getString(TAG_LNG); 
       Log.v(lng, "longitude"); 
       cuisineString = e.getString(TAG_COUSINE); 
       timingsString = e.getString(TAG_TIMING); 
       // costString = e.getString(TAG_COSRTWO); 

       res_des_String = e.getString(TAG_RES_DES); 

       cardString = e.getString(TAG_CARDS); 
       facilitiesString = e.getString(TAG_FACILITIES); 

       parkingString = e.getString(TAG_PARKING); 
       minorderString = e.getString(TAG_MINORDER); 
       deliverychargesString = e.getString(TAG_DELIVERY_CHARGES); 
       ratings = e.getString(TAG_RATING); 
       smily = e.getString(TAG_SMILY); 
       s_o_t = e.getString(TAG_S_O); 
       s_c_t = e.getString(TAG_S_C); 
       m_o_t = e.getString(TAG_M_O); 
       m_c_t = e.getString(TAG_M_C); 
       t_o_t = e.getString(TAG_T_O); 
       t_c_t = e.getString(TAG_T_C); 
       w_o_t = e.getString(TAG_W_O); 
       w_c_t = e.getString(TAG_W_C); 
       th_o_t = e.getString(TAG_TH_O); 
       th_c_t = e.getString(TAG_TH_C); 
       f_o_t = e.getString(TAG_F_O); 
       f_c_t = e.getString(TAG_F_C); 
       sat_o_t = e.getString(TAG_SAT_O); 
       sat_c_t = e.getString(TAG_SAT_C); 
       smily = e.getString(TAG_SMILY); 

      } 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(String result) { 

     super.onPostExecute(result); 
     // progressBar.setVisibility(View.GONE); 
     progressDialog.dismiss(); 
     detail.setVisibility(View.VISIBLE); 
     imageLoader.DisplayImage(logo, reslogo); 
     resname.setText(StringEscapeUtils.unescapeHtml(name)); 
     toptxt.setText(StringEscapeUtils.unescapeHtml(name)); 
     address.setText(StringEscapeUtils.unescapeHtml(street) + " \n" 
       + StringEscapeUtils.unescapeHtml(localityString) + " " 
       + StringEscapeUtils.unescapeHtml(city_state)); 
     cuisineString = (cuisineString.equals("") ? "Not present" 
       : StringEscapeUtils.unescapeHtml(cuisineString)); 
     cuisine.setText(cuisineString); 
     if (dayOfTheWeek.equalsIgnoreCase("monday")) { 
      timings.setText(m_o_t + " - " + m_c_t); 

     } else if (dayOfTheWeek.equalsIgnoreCase("tuesday")) { 
      timings.setText(t_o_t + " - " + t_c_t); 
     } else if (dayOfTheWeek.equalsIgnoreCase("wednesday")) { 
      timings.setText(w_o_t + " - " + w_c_t); 
     } else if (dayOfTheWeek.equalsIgnoreCase("thursday")) { 
      timings.setText(th_o_t + " - " + th_c_t); 
     } else if (dayOfTheWeek.equalsIgnoreCase("friday")) { 
      timings.setText(f_o_t + " - " + f_c_t); 
     } else if (dayOfTheWeek.equalsIgnoreCase("saturday")) { 
      timings.setText(sat_o_t + " - " + sat_c_t); 
     } else if (dayOfTheWeek.equalsIgnoreCase("sunday")) { 
      timings.setText(s_o_t + " - " + s_c_t); 
     } 
     // costfortwo.setText(costString); 
     res_des_String = (res_des_String.equals("") ? "Not present" 
       : StringEscapeUtils.unescapeHtml(res_des_String)); 
     cardString = (cardString.equals("") ? "Not present" 
       : StringEscapeUtils.unescapeHtml(cardString)); 
     parkingString = (parkingString.equals("") ? "Not present" 
       : StringEscapeUtils.unescapeHtml(parkingString)); 
     facilitiesString = (facilitiesString.equals("") ? "Not present" 
       : StringEscapeUtils.unescapeHtml(facilitiesString)); 
     minorderString = (minorderString.equals("") ? "Not present" 
       : StringEscapeUtils.unescapeHtml(minorderString)); 
     deliverychargesString = (deliverychargesString.equals("") ? "Not present" 
       : StringEscapeUtils.unescapeHtml(deliverychargesString)); 

     res_description.setText(res_des_String.replaceAll(
       "(\r\n\t|\n\t|\n|\t)", "")); 
     cards.setText(cardString); 
     facilities.setText(facilitiesString); 

     parking.setText(parkingString); 
     minoredr.setText(minorderString); 
     delivery_charges.setText(deliverychargesString); 
     if (ratings.equals("")) { 
      rating.setVisibility(View.GONE); 
     } else { 
      DecimalFormat numberFormat = new DecimalFormat("##.##"); 
      rating.setText(numberFormat.format(Double.valueOf(ratings))); 
     } 
     if (smily.equals("1")) { 
      smiley.setImageResource(R.drawable.happy_smile); 
     } else if (smily.equals("0")) { 
      smiley.setImageResource(R.drawable.sad_smile); 
     } else { 
      smileyLayout.setVisibility(View.GONE); 
     } 

    } 
} 

二等具有Finish按鈕

setContentView(R.layout.submenulist); 

    Intent i = getIntent(); 
    main_menu = i.getStringExtra("id"); 
    res_id = i.getStringExtra("res_id"); 
    res_name = i.getStringExtra("res_name"); 
    Log.v("Main Menu Name", main_menu); 
    fillDatabase = new DatabaseHandler(getApplicationContext()); 
    fillDatabase.open(); 
    count = fillDatabase.fetchCount(); 


    backButton = (Button) findViewById(R.id.finish); 
    backButton.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      finish(); 
     } 
    }); 



} 

次活動有地按下前臺活動完成,但完成這樣的背景後按鈕活動呼叫Acync任務再次執行。如何重新擁有這一點。

非常感謝

+0

您可以發佈以下一些代碼,請:您的AsyncTask代碼,你怎麼啓動前臺活動以及您的AndroidManifest活動的[launchMode](http://developer.android.com/guide/topics/manifest/activity-element.html#lmode)? – verybadalloc

+0

發佈您的代碼.. – bakriOnFire

+0

我不希望Asych Task在我從前臺活動中按回按鈕時再次執行。我沒有代碼,但想知道它的概念 –

回答

0

然後開始你的AsyncTask在第一個活動的onCreate方法當第二個活動開始第一次活動將成爲前景和當可見光第二活性finjsh然後第一個活動不會重新創建將只會恢復暫停活動,所以你在創建asynctask時不會再次啓動線程

+0

是的,你是對的,但是我在完成前景活動之後在oncreate中調用執行,它在完成前景活動時重新執行 –

+0

是第一次活動還是第二次活動? –

+0

第二個活動 –

0

嗯,這是你應達到使用Singleton類

  1. 創建一個類說的DataManager

    public class DataManager { 
    public static DataManager dManager; 
    
    boolean isDataRecieved; 
    
    public static DataManager getInstance() 
    { 
        if(dManager!=null) 
         return dManager; 
    
        else 
         dManager=new DataManager(); 
    
        return dManager; 
    } 
    
    } 
    

這裏getInstance方法可確保只有一個該類的對象並且在解析和存儲數據後立即將isDataRecieved布爾型設置爲true。

喜歡上你的AsyncTask的postExecute添加這些行

Datamanager d=Datamanager.getInstance(); 
d.isDataRecieved=true; 
現在

上的onCreate添加這些行

Datamanager d=Datamanager.getInstance(); 
if(!d.isDataRecieved) 
{ 
//This means data is not recieved 

execute your asynctask else not 
} 
+0

你試過這個嗎? –

+0

不能使用這個... –

+0

代碼已發佈。請看看 –