How to use FragmentManager to substitute the deprecated showDialog

左心房为你撑大大i 提交于 2019-12-10 22:36:44

问题


I am using the method displayDialog... it works but Eclipse shows me a warning on this:

showDialog(showRetry ? 1 : 0);

Method:

   private void displayDialog(final boolean showRetry) {
                    mHandler.post(new Runnable() {
                        public void run() {
                            setProgressBarIndeterminateVisibility(false);
                            showDialog(showRetry ? 1 : 0);
                        }
                    });
                } 

Searching I have found similar question in which seen that now there is the FragmentManager that can be used to avoid the problem, but I haven't understand how I could use to fix the problem in my specific scenario maintaining retro-compatibility with Froyo and GingerBread.

Could you help me?

The full class code is:

 public class Home extends Activity {
        private static final String BASE64_PUBLIC_KEY = "mykeywithoutspaces";

        // Generate your own 20 random bytes, and put them here.
        private static final byte[] SALT = new byte[] {
            -00, 00, 30, -2, -58, -57, 00, -64, 51, 32, -95, -45, 13, -6, -36, -15, -11, 32, -64,
            89
        };

        private TextView mStatusText;
        private Button mCheckLicenseButton;

        private LicenseCheckerCallback mLicenseCheckerCallback;
        private LicenseChecker mChecker;
        // A handler on the UI thread.
        private Handler mHandler;

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

            mStatusText = (TextView) findViewById(R.id.status_text);
buttonNewDoc = (ImageButton) findViewById(R.id.btNewDoc);
        buttonNewDoc.setOnClickListener(mNewDoc);
            mCheckLicenseButton = (Button) findViewById(R.id.check_license_button);
            mCheckLicenseButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    doCheck();
                }
            });

            mHandler = new Handler();

            // Try to use more data here. ANDROID_ID is a single point of attack.
            String deviceId = Secure.getString(getContentResolver(), Secure.ANDROID_ID);

            // Library calls this when it's done.
            mLicenseCheckerCallback = new MyLicenseCheckerCallback();
            // Construct the LicenseChecker with a policy.
            mChecker = new LicenseChecker(
                this, new ServerManagedPolicy(this,
                    new AESObfuscator(SALT, getPackageName(), deviceId)),
                BASE64_PUBLIC_KEY);
            doCheck();
        }

        protected Dialog onCreateDialog(int id) {
            final boolean bRetry = id == 1;
            return new AlertDialog.Builder(this)
                .setTitle(R.string.unlicensed_dialog_title)
                .setMessage(bRetry ? R.string.unlicensed_dialog_retry_body : R.string.unlicensed_dialog_body)
                .setPositiveButton(bRetry ? R.string.retry_button : R.string.buy_button, new DialogInterface.OnClickListener() {
                    boolean mRetry = bRetry;
                    public void onClick(DialogInterface dialog, int which) {
                        if ( mRetry ) {
                            doCheck();
                        } else {
                            Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(
                                    "http://market.android.com/details?id=" + getPackageName()));
                                startActivity(marketIntent);                        
                        }
                    }
                })
                .setNegativeButton(R.string.quit_button, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                }).create();
        }

        private void doCheck() {
buttonNewDoc.setEnabled(false);
            mCheckLicenseButton.setEnabled(false);
            setProgressBarIndeterminateVisibility(true);
            mStatusText.setText(R.string.checking_license);
            mChecker.checkAccess(mLicenseCheckerCallback);
        }

        private void displayResult(final String result) {
            mHandler.post(new Runnable() {
                public void run() {
                    mStatusText.setText(result);
                    setProgressBarIndeterminateVisibility(false);
                    mCheckLicenseButton.setEnabled(true);
                }
            });
        }

        private void displayDialog(final boolean showRetry) {
            mHandler.post(new Runnable() {
                public void run() {
                    setProgressBarIndeterminateVisibility(false);
                    showDialog(showRetry ? 1 : 0);
                }
            });
        }    

        private class MyLicenseCheckerCallback implements LicenseCheckerCallback {
            public void allow(int policyReason) {
                if (isFinishing()) {
                    // Don't update UI if Activity is finishing.
                    return;
                }

                // Should allow user access.
                displayResult(getString(R.string.allow));
buttonNewDoc.setEnabled(false);
            }

            public void dontAllow(int policyReason) {
                if (isFinishing()) {
                    // Don't update UI if Activity is finishing.
                    return;
                }
                displayResult(getString(R.string.dont_allow));
                // Should not allow access. In most cases, the app should assume
                // the user has access unless it encounters this. If it does,
                // the app should inform the user of their unlicensed ways
                // and then either shut down the app or limit the user to a
                // restricted set of features.
                // In this example, we show a dialog that takes the user to Market.
                // If the reason for the lack of license is that the service is
                // unavailable or there is another problem, we display a
                // retry button on the dialog and a different message.
                displayDialog(policyReason == Policy.RETRY);
            }

            public void applicationError(int errorCode) {
                if (isFinishing()) {
                    // Don't update UI if Activity is finishing.
                    return;
                }
                String result = String.format(getString(R.string.application_error), errorCode);
                displayResult(result);
            }
        }

        @Override
        protected void onDestroy() {
            super.onDestroy();
            mChecker.onDestroy();
        }

    }

回答1:


Your activity should subclass FragmentActivity, which can be found in the v4 support library, which you should add to your project. You then can get a reference to the FragmentManager using getSupportFragmentManager() to show a DialogFragment. You'll have to create a DialogFragment class with the layout and the logic that your dialog requires. Here's how you'd show it from your FragmentActivity:

FragmentManager fm = getSupportFragmentManager();
YourDialogFragment yourDialog = new YourDialogFragment();
yourDialog.show(fm, "some_optional_tag");

Here's an overview: http://android-developers.blogspot.com/2012/05/using-dialogfragments.html




回答2:


What google seems to use in this example is to change the "protected Dialog onCreateDialog (int id) {" so that it is called internally always which is called "showDialog (showRetry? 1: 0);" creating default dialog for retry/cancel.

I suggest to use the following code, creating a handy method that creates the standard dialog, and changing or removing the "onCreateDialog()" as this becomes unnecessary.

protected Dialog onCreateDialog(int id) {
    return CreateDialogRetryCancel(id);
}

public Dialog CreateDialogRetryCancel(int id) {
    final boolean bRetry = id == 1;
    return new AlertDialog.Builder(this)
        .setTitle(R.string.unlicensed_dialog_title)
        .setMessage(bRetry ? R.string.unlicensed_dialog_retry_body : R.string.unlicensed_dialog_body)
        .setPositiveButton(bRetry ? R.string.retry_button : R.string.buy_button, new DialogInterface.OnClickListener() {
            boolean mRetry = bRetry;
            public void onClick(DialogInterface dialog, int which) {
                if ( mRetry ) {
                    doCheck();
                } else {
                    Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(
                            "http://market.android.com/details?id=" + getPackageName()));
                        startActivity(marketIntent);                        
                }
            }
        })
        .setNegativeButton(R.string.quit_button, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        }).create();
}

private void displayDialog(final boolean showRetry) {
    mHandler.post(new Runnable() {
        public void run() {
            setProgressBarIndeterminateVisibility(false);
            CreateDialogRetryCancel(showRetry ? 1 : 0).show();
            //showDialog(showRetry ? 1 : 0);
            mCheckLicenseButton.setEnabled(true);
        }
    });
}    



回答3:


FragmentManager fm = getSupportFragmentManager();
yourDialog.show(fm, "some_optional_tag");

The getSupportFragmentManager() was accepted only when the correct class is imported while making DialogFragment i.e import android.support.v4.app.DialogFragment



来源:https://stackoverflow.com/questions/12710092/how-to-use-fragmentmanager-to-substitute-the-deprecated-showdialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!