0

我有一个显示对话框的 AsyncTask。我不希望它在设备旋转时消失。

我的解决方案是将它存储在 Application 类中,这样当 Activity 死亡时它就不会死亡。当活动结束时,我只是关闭对话框,当它恢复时,我只是显示对话框。

这似乎有效。当设备旋转时,Dialog 消失,然后重新出现新的 Activity。唯一的问题是,旋转后对话框不再动画。更奇怪的是,重新旋转到原来的方向会导致动画从它停止的地方继续。

为什么会这样?我该如何解决这个问题?卡塔尔

public class OrientationActivity extends Activity {
    MyApplication application;

    public class LongOperation extends AsyncTask<String, Void, String> {
          private ProgressDialog dialog;

          public LongOperation(Context context) {
              dialog = new ProgressDialog(context);
              dialog.setCanceledOnTouchOutside(false);
          }

          @Override
          protected void onPreExecute() {
              this.dialog.setMessage("Busy!");
              this.dialog.show();
          }

          @Override
          protected String doInBackground(String... params) {
              while(true){

              }
          }      

          @Override
          protected void onPostExecute(String result) {                           
              if(dialog != null && dialog.isShowing())
                  dialog.dismiss();
          }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        application = (MyApplication) getApplication();

        if(application.longOperation == null) {
            application.longOperation = new LongOperation(this);
            application.longOperation.execute();
        }
    }

    @Override
    public void onPause() {
        super.onPause();

        application.longOperation.dialog.dismiss();
    }

    @Override
    public void onResume() {
        super.onResume();

        application.longOperation.dialog.show();
    }
}

public class MyApplication extends Application 
{
    LongOperation longOperation = null;
}
4

2 回答 2

0

您是否尝试添加 onConfigurationChanged?

@Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);

            if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){

                Log.e("TAG","LANDSCAPE");

            }else{

                Log.e("TAG","PORTRAIT");

            }


        }

如果你添加这个,你可以处理方向,如果你没有添加这个,如果你改变了方向,活动将在你添加后重新创建,活动不会被重新创建,但会保持当前状态与之前的方向相同

如果您有任何问题,请随时在评论中问我:)

于 2012-11-14T08:53:37.883 回答
0

好的,看来以下是最好的解决方案。欢迎评论这是否被认为是最佳实践。请注意,我已包含 v4 支持库以实现向后兼容性。

我的活动

package ie.cathalcoffey.android.orientation;

import android.content.Context;
import android.content.res.Configuration;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;

public class OrientationActivity extends FragmentActivity implements FireMissilesDialogFragment.NoticeDialogListener{
    MyApplication application;
    boolean rotated;
    public class LongOperation extends AsyncTask<String, Void, String> {
          private FireMissilesDialogFragment dialog;

          public LongOperation(Context context) {
              dialog = new FireMissilesDialogFragment();
              dialog.show(getSupportFragmentManager(), "");
          }

          @Override
          protected void onCancelled() {
              if(dialog != null && dialog.isVisible())
                  dialog.dismiss();
          }

          @Override
          protected void onPreExecute() {
          }

          @Override
          protected String doInBackground(String... params) {
              while(true){

                  if(this.isCancelled())
                      break;

              }

              return null;
          }      

          @Override
          protected void onPostExecute(String result) {                           

          }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        application = (MyApplication) getApplication();

        if(application.longOperation == null) {
            application.longOperation = new LongOperation(this);
            application.longOperation.execute();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        if(application.longOperation != null)
        {
            application.longOperation.cancel(true);
            application.longOperation = null;
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public void onDialogPositiveClick(DialogFragment dialog) {
        if(application.longOperation != null)
        {
            application.longOperation.cancel(true);
            application.longOperation = null;
        }
    }

    @Override
    public void onDialogNegativeClick(DialogFragment dialog) {
        if(application.longOperation != null)
        {
            application.longOperation.cancel(true);
            application.longOperation = null;
        }
    }
}

我的对话片段

package ie.cathalcoffey.android.orientation;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;

public class FireMissilesDialogFragment extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage("Whatever")
               .setPositiveButton("fire", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       mListener.onDialogPositiveClick(FireMissilesDialogFragment.this);
                   }
               })
               .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       mListener.onDialogNegativeClick(FireMissilesDialogFragment.this);
                   }
               });
        // Create the AlertDialog object and return it
        return builder.create();
    }

    /* The activity that creates an instance of this dialog fragment must
     * implement this interface in order to receive event callbacks.
     * Each method passes the DialogFragment in case the host needs to query it. */
    public interface NoticeDialogListener {
        public void onDialogPositiveClick(DialogFragment dialog);
        public void onDialogNegativeClick(DialogFragment dialog);
    }

    // Use this instance of the interface to deliver action events
    NoticeDialogListener mListener;

    // Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        // Verify that the host activity implements the callback interface
        try {
            // Instantiate the NoticeDialogListener so we can send events to the host
            mListener = (NoticeDialogListener) activity;
        } catch (ClassCastException e) {
            // The activity doesn't implement the interface, throw exception
            throw new ClassCastException(activity.toString()
                    + " must implement NoticeDialogListener");
        }
    }
}

我的应用程序

package ie.cathalcoffey.android.orientation;
import ie.cathalcoffey.android.orientation.OrientationActivity.LongOperation;
import android.app.Application;


public class MyApplication extends Application 
{
    LongOperation longOperation = null;
}

最后我的清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ie.cathalcoffey.android.orientation"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />

    <application
        android:name=".MyApplication"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".OrientationActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
于 2012-11-14T12:42:37.607 回答