質問

Hey guys I am building an app that loads data from mysql through json. While data loads, a progress dialog is showing. When the loading is complete the dialog dismisses. But when I change screen rotation it shows up again and it loads the data again. How can I prevent this from happening??. And also the same happens to all alert dialogs that I use. Any help please?

My code:

@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);
        }
    }

I finally fixed it. it just needed as well in the manifest.

android:configChanges="orientation|keyboardHidden|screenSize"

Any help will be welcomed and much appreciated.

役に立ちましたか?

解決

In your manifest, in an activity/fragment activity tag, please add the following,

android:configChanges="orientation"

Above line will notify your activity/fragmentactivity when there is an orientation change. In order for you to do something on it, you need to override the following method in your activity,

 public void onConfigurationChanged(Configuration newConfig) {

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

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

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

}

Hope this helps

EDIT:

Please see the complete code below,

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;
        }

    }



}

Manifest:

<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>

This is what I did. I just forgot to dismiss a dialog properly. But rest is working. However if you are using a fragment then that might not work for you as it is for an activity or a Fragment Activity.

他のヒント

You have 2 ways:

  • Retaining the state on the configuration change. This means that you would need to store the current state of your Fragment in the savedInstance Bundle and restore its state once the change has happened. This would imply that you don't call again the .show() on your ProgressDialog object, but keep its state. This is the recommended way to go.

  • Handle the configuration change yourself. This will prevent Android from restarting your Activity/Fragment during certain configuration changes and receive a callback when the configurations change, this way you can manually update your activity as necessary. This is the discouraged way to go and should be used as the last resource.

Source (and very recommended reading): http://developer.android.com/guide/topics/resources/runtime-changes.html

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top