2017-01-10 8 views
0

新しいActivityを開くと、TextViewをリアルタイムで更新するために新しいAsyncTaskを実行します。 Activityが終了の場合AsyncTaskがキャンセルされます。このActivityを2回開いて閉じると、すべて正常に動作します。しかし、3回目とそれ以外の間に、doInBackgroundメソッドは起動されません。そのタスクのステータスは「実行中」です。コードはこのように見えます。なぜAsyncTaskが正しく2回しか動作しないのですか?

オープン活動:

list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id){ 
      final String selectedFromList = (list.getItemAtPosition(position).toString()); 

      Context context = getApplicationContext(); 
      Intent intent = new Intent(context, Main2Activity.class); 
      intent.putExtra("NetworkText", txts.getText()); 
      intent.putExtra("ChooseCompTitle", new String(selectedFromList)); 
      startActivity(intent); 
      overridePendingTransition(R.anim.right_in, R.anim.left_out); 
     } 
    }); 

"のonCreate" を実行開始AsyncTask:アクティビティがあるとき、私はこのAsyncTaskを閉じる必要がありますので、

public void finish() { 
    super.finish(); 
    if(!taskUpdateText.isCancelled()) { 
     Log.i("LogX", "End AsyncTask!"); 
     taskUpdateText.cancel(true); 
    } 
    else Log.i("LogX", "Asynctask is already cancell!"); 
    overridePendingTransition(R.anim.left_in, R.anim.right_out); 
} 

:私もfinishメソッドをオーバーライドし

ConnectionDataPack supportDesktopData; 
UpdateTextView taskUpdateText; 

protected void onCreate(Bundle savedInstanceState) { 
    . . . . 

    final TextView txts = (TextView) findViewById(R.id.textView2); 
    final ScrollView scroll = (ScrollView) findViewById(R.id.ScrollView01); 
    final TextView textContent = (TextView) findViewById(R.id.textView4); 

    if(getIntent().getExtras().getString("NetworkText") != null){ 
     txts.setText(getIntent().getExtras().getString("NetworkText")); 
    } 

    if(getIntent().getExtras().getString("ChooseCompTitle") != null){ 
     supportDesktopData = findFitCompPack(getIntent().getExtras().getString("NetworkText"), MainActivity.connectComputer); 
    } 
    else finish(); 

    taskUpdateText = new UpdateTextView(supportDesktopData, textContent, scroll); 
    taskUpdateText.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 

    if(taskUpdateText.getStatus() == AsyncTask.Status.PENDING){ 
     Log.i("LogsX", "AsyncTask has not started yet."); 
    } 
    if(taskUpdateText.getStatus() == AsyncTask.Status.RUNNING){ 
     Log.i("LogsX", "AsyncTask is currently doing work."); 
    } 
    if(taskUpdateText.getStatus() == AsyncTask.Status.FINISHED){ 
     Log.i("LogsX", "AsyncTask is done"); 
    } 
    . . . . 

} 

終わり。

AsyncTaskは、次のようになります。これらと

class UpdateTextView extends AsyncTask<Void, Void, Void> 
{ 
    int lengthTextBuffor = 0; 

    ConnectionDataPack compDataPack; 
    TextView textUpdate; 
    ScrollView scrool; 

    public UpdateTextView(ConnectionDataPack compDataPack, TextView textUpdate, ScrollView scrool) 
    { 
     this.compDataPack = compDataPack; 
     this.textUpdate = textUpdate; 
     this.scrool = scrool; 
    } 

    protected void onProgressUpdate(Void... params) { 
      textUpdate.setText(compDataPack.dataBackupCharacter.toString()); 
      scrool.fullScroll(View.FOCUS_DOWN); 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 

     Log.i("LogX", "doInBackground Starts!"); 
     boolean startLoop = true; 
     while(true) 
     { 
       synchronized (compDataPack.lock){ 
       if(!startLoop && lengthTextBuffor == compDataPack.dataBackupCharacter.length()) { 
        try { 
         compDataPack.lock.wait(); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
       } 
       startLoop = false; 
       publishProgress(); 
      } 
     } 
    } 
} 
+0

がすべての答えをありがとう、しかし、以下のコードを置きますそれは私がここで解決策を見つけたようです:[リンク](http://stackoverflow.com/a/16851515/7392319)。それは今のところ働きます。 – sQanns

答えて

0

問題は、あなたのasyntaskがガベージコレクションを取得しますないか、それはまだバックグラウンドで実行されているかもしれません。

if (taskUpdateText != null && taskUpdateText.getStatus() == AsyncTask.Status.PENDING) { 
      taskUpdateText.execute(); 
     } else if (taskUpdateText == null || taskUpdateText.getStatus() == AsyncTask.Status.FINISHED) { 
      taskUpdateText = new UpdateTextView(); 
      taskUpdateText.execute(); 
     } 

とonDestroyで..コードの下に試してみてくださいコードの下に

@override 
public void onBackPressed() { 
if(taskUpdateText!=null) { 
      taskUpdateText.cancel(true); 
      taskUpdateText = null; 
     } 

} 
0

を使用しようとは)

@Override 
    public void onDestroyView() { 
     super.onDestroyView(); 
     if (taskUpdateText != null && taskUpdateText.getStatus() == AsyncTask.Status.RUNNING) { 
      taskUpdateText.cancel(true); 
     } 
    } 
関連する問題