我有一个显示对话框的AsyncTask。旋转设备时,我不希望它消失。
我的解决方案是将它存储在Application类中,这样当Activity死亡时它不会死掉。当活动消失时,我只是关闭对话框,当它恢复时我只是显示对话框。
这似乎有效。旋转设备后,对话框将消失,然后随新活动重新出现。唯一的问题是旋转后Dialog不再动画。更奇怪的是,重新旋转到原始方向会导致动画从停止的位置继续。
为什么会这样?我该如何解决这个问题? 卡瑟尔
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;
}
答案 0 :(得分: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");
}
}
如果你添加了这个,你可以处理方向,如果你没有添加这个,如果你改变了方向,你将在重新添加后重新创建活动,不会重新创建活动,但会保持当前状态与以前的方向
如果您有任何问题,请随时在评论中提问我:)
答案 1 :(得分: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;
}
}
}
我的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>