大家好,我正在构建一个通过json从mysql加载数据的应用程序。加载数据时,将显示一个进度对话框。加载完成后,对话框消失。但是,当我更改屏幕旋转角度时,它将再次显示并再次加载数据。如何防止这种情况发生?我使用的所有警报对话框也会发生同样的情况。有什么帮助吗?

我的代码:

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub

        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            if(!pDialog.isShowing()){
                pDialog.show();
            }else{
                pDialog.dismiss();
            }
            if(!onAboutPressed().isShowing()){
                onAboutPressed().show();
            }else{
                onAboutPressed().dismiss();
            }
            if(!onAlertMobileData().isShowing()){
                onAlertMobileData().show();
            }else{
                onAlertMobileData().dismiss();
            }
            super.onConfigurationChanged(newConfig);
        }
        if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            if(!pDialog.isShowing()){
                pDialog.show();
            }else{
                pDialog.dismiss();
            }
            if(!onAboutPressed().isShowing()){
                onAboutPressed().show();
            }else{
                onAboutPressed().dismiss();
            }
            if(!onAlertMobileData().isShowing()){
                onAlertMobileData().show();
            }else{
                onAlertMobileData().dismiss();
            }
            super.onConfigurationChanged(newConfig);
        }
    }


我终于解决了。清单中也需要它。

android:configChanges="orientation|keyboardHidden|screenSize"


任何帮助都将受到欢迎和赞赏。

最佳答案

在清单中的活动/片段活动标签中,添加以下内容,

android:configChanges="orientation"


当方向发生变化时,上面的行将通知您的活动/片段活动。为了使您可以执行某些操作,您需要在活动中覆盖以下方法,

 public void onConfigurationChanged(Configuration newConfig) {

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

    Log.e("On Config Change","LANDSCAPE");
}else{

    Log.e("On Config Change","PORTRAIT");
}

}


希望这可以帮助

编辑:

请参阅下面的完整代码,

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.res.Configuration;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.example.android.customviews.R;

public class TestActivity extends FragmentActivity {

    Dialog mDialog = null;

    @Override
    protected void onCreate(Bundle pSavedInstanceState) {
        super.onCreate(pSavedInstanceState);
        setContentView(R.layout.activity_calendar);

        ((Button) findViewById(R.id.openButton)).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View pV) {
                mDialog = ProgressDialog.show(TestActivity.this, "", "Please Wait...");
                new WasteTime().execute(null, null, null);
            }
        });

    }

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

        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
        }
        if (pNewConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {

            setContentView(R.layout.activity_camera);
            Log.e("On Config Change", "LANDSCAPE");
        }
    }

    class WasteTime extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... pParams) {

            final int timeWaster = 10000;
            for (int i = 0; i < timeWaster; i++) {
                Log.e("Wasting Time", i + "milliseconds wasted");
            }

            return null;
        }

    }



}


表现:

<activity
            android:name="com.example.android.activities.TestActivity"
            android:theme="@style/ApplicationTheme_ActionBar"
            android:configChanges="orientation">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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


这就是我所做的。我只是忘了正确消除对话。但是休息是有效的。但是,如果您使用的是片段,那么它可能不适用于您,因为它适用于活动或片段活动。

10-07 19:47
查看更多