开发者

Link in AlertDialog leaks window?

开发者 https://www.devze.com 2023-03-29 04:18 出处:网络
I\'ve created a linkable text in an alert dialog, and make the TextView clickable, like this: final SpannableString noRecords = new SpannableString(\"Sorry, no records could be found, please try agai

I've created a linkable text in an alert dialog, and make the TextView clickable, like this:

final SpannableString noRecords = new SpannableString("Sorry, no records could be found, please try again, or contact us at 867-5309");
Linkify.addLinks(noRecords);

AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("No Records Found")
    .setMessage(noRecords)
    .setCancelable(true)
    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    });

AlertDialog alert = builder.create();
alert.show();

((TextView)alert.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());

This works, however when it's clicked it produces an error in logcat:

08-19 19:40:55.753: ERROR/WindowManager(5886): Activity com.blah.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405d7010 that was originally added here 08-19 19:40:55.753: ERROR/WindowManager(5886): android.view.WindowLeaked: Activity com.blah.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405d7010 that was originally added here

I think this is because the alert is not dismisse开发者_StackOverflow社区d before the link is clicked. Is there any way around this? I'd prefer not to be throwing any errors.


If you want to dispose the dialog before the activity is destroyed, you can override the onDestroy() event

@Override
public void onDestroy(){
    //Your dialog disposal code here

    super.onDestroy(); //Make sure you include this at the end.
}

Hope that helps

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号