Question

I am using Android annotation.There is a progress dialog but when i am rotating the screen (land to port) . The progress dialog dismiss and showing these error's in log cat.

Activity com.example.progressdialog.AnnotationProgressDialogActivity_ has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40661c20 that was originally added here

android.view.WindowLeaked: Activity com.example.progressdialog.AnnotationProgressDialogActivity_ has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40661c20 that was originally added here

and here is my code:-

@EActivity(R.layout.main)
public class AnnotationProgressDialogActivity extends Activity 
{
    @NonConfigurationInstance
    ProgressDialog pd ;
        
    @NonConfigurationInstance
    @Bean 
    BackgroundTask bgt;
     
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

    }
     
    @Click
    public void loginButton()
    {
        
        pd = ProgressDialog.show(this,"Login", "Loading please wait.....");
        bgt.backGroundMethod();
    }
    
    public void dismissDialog()
    {
        pd.dismiss();
    }
}

    enter code here

@EBean
public class BackgroundTask 
{
    @RootContext
    Activity apda;
    
    @Background
    public void backGroundMethod()
    {
        try
        {
            TimeUnit.SECONDS.sleep(10);
            update();
        }
        catch(Exception e)
        {
            Log.e("Error",""+e);
        }
}

    @UiThread
    public void update()
    {
        //((AnnotationProgressDialogActivity)apda).updateUI();
        ((AnnotationProgressDialogActivity)apda).dismissDialog();
    }
}

   
Était-ce utile?

La solution

This error appears because you are keeping a reference to the ProgressDialog that is itself keeping a reference to the destroyed activity that has a reference to the leaked DecorView.

Here is what you should do instead:

  1. Have a way to mark BackgroundTask as "running", such as a boolean field
  2. in onCreate(), check is "running" is true, and if yes, create a new progress dialog and show it

Also notice that @RootContext Activity apda can be replaced with @RootContext AnnotationProgressDialogActivity apda.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top