0

写真をリモートサーバーにアップロードするために非同期タスクを使用しています。ユーザーに進行状況を表示するために、進行状況ダイアログを使用しています。この進行状況ダイアログは、非同期タスク クラスの preExecute メソッドで作成されます。

ユーザーが画面を回転すると、進行状況ダイアログは消えますが、非同期タスクはまだ実行されています。アクティビティは非同期タスクへの静的参照を保持しているため、タスクがまだ実行中であることがわかります。進行状況ダイアログを再度表示するにはどうすればよいですか? 私は次のことを試しました:

アクティビティの create メソッド:

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

if(task != null) {
Status status = task.getStatus();

if(status == Status.RUNNING) {
    task.updateViewContext(this);
}

}

私の非同期タスククラスのコンストラクター:

public UploadTask(UploadPicturesActivity activity) {
   this.context = activity;
   dialog = new ProgressDialog(activity);
   dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);        
}   

私の asnyc タスク クラスの preexcetue メソッド:

protected void onPreExecute() {
     this.dialog.show();
}

ダイアログを再度表示しようとしています(機能しません):

public void updateViewContext(UploadPicturesActivity activity) {
    this.context = activity;
    dialog.show();
}

よろしく

マイケル

4

2 に答える 2

2

android:configChanges="orientation"ファイルのアクティビティ宣言に属性を追加してみませんかAndroidManifest.xml

この属性の目的は、android:configChanges実際に必要なときにアクティビティが再作成されるのを防ぐことです。

変更時にダイアログを閉じませんscreen orientation

Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device 
switches between portrait and landscape orientation. Thus, if you want to prevent runtime 
restarts due to orientation change when developing for API level 13 or higher (as declared by the 
minSdkVersion and targetSdkVersion attributes), you must include the "screenSize" value in 
addition to the "orientation" value.That is, you must declare android:configChanges="orientation|screenSize"
于 2013-02-24T18:27:46.097 に答える
1

これは、Android がアクティビティを破棄し、ローテーションで再開するためです。これは、すべてのビュー、ダイアログなどが破棄されることを意味します。しかし、実行中のスレッドはできません。これに対する最善の解決策はandroid:configChanges="orientation|screenSize"、マニフェストのアクティビティに追加することです。これにより、アクティビティのこの動作がオフになります。ただし、横向きと縦向きでレイアウトが異なる場合を除き、アクティビティは正しく再描画されます。

于 2013-02-24T18:28:10.087 に答える