さて、以下が最善の解決策のようです。これがベストプラクティスと見なされるかどうかについてのコメントを歓迎します。下位互換性のために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;
}
}
}
私のDialogFragment
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>