Boy
Boy

Reputation: 435

android - MainActivity has leaked window

I got this error message whenever I press back from/to MainActivity. The app won't crash though but this message will be shown in logcat. I have searched about this issue but most of them related to dismissing dialog. But my case is I have no dialog displayed on MainActivity.

12-06 18:18:24.799  13422-13422/com.zq.x E/WindowManager﹕ android.view.WindowLeaked: Activity com.zq.x.MainActivity has leaked window android.widget.RelativeLayout{42e869e0 I.E..... ......ID 0,0-768,96} that was originally added here
            at android.view.ViewRootImpl.<init>(ViewRootImpl.java:348)
            at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
            at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
            at uk.co.senab.actionbarpulltorefresh.library.PullToRefreshAttacher.addHeaderViewToActivity(PullToRefreshAttacher.java:616)
            at uk.co.senab.actionbarpulltorefresh.library.PullToRefreshAttacher$1.run(PullToRefreshAttacher.java:127)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

activity_main.xml

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" />

MainActivity.java

...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }
        });

        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            actionBar.addTab(
                    actionBar.newTab()
                            .setText(mSectionsPagerAdapter.getPageTitle(i))
                            .setTabListener(this));
        }

        session = new SessionManager(getApplicationContext());

        nearbyUsers = new ArrayList<User>();

        if(!session.isValidated()){ 
            session.loginUser();
            finish();
        }else{
            Log.d("MainActivity","Logged in as ["+session.getUserDetails().get(SessionManager.KEY_USR_EMAIL)+"]");

            gps = new GPSTracker(getApplicationContext());
            if(gps.canGetLocation()){
                gps.addListener(this);
                if(!gps.isNetworkEnabled() && gps.isGPSEnabled() && mUpdateLocTask == null){
                    this.locationUpdated();
                }
            }
        }
    }
...

Upvotes: 0

Views: 1547

Answers (1)

Jimmy Ilenloa
Jimmy Ilenloa

Reputation: 2009

I had this issue as well. Did a work around for it and it worked for me.

The PullToRefreshLayout (extending RelativeLayout) class actually dynamically adds the RelativeLayout to the main activity. I don't understand everything there is to it. However, one thing I can tell you is that the RelativeLayout needs to be destroyed as the user exits the main activity.

Destroying the relative layout is actually already handled by the PullToRefresh, but fails in my case at some point. I had to manually destroy the RelativeLayout view within the onDetachedFromWindow() of my Main Activity.

I modified the PullToRefreshLayout.java in my library and added:

public void manuallyDetachFromWindow(){
    onDetachedFromWindow();
}

In the MainActivity.java, I added below code:

public static WeakReference<PullToRefreshLayout> wrefMPullToRefreshLayout;
...

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    wrefMPullToRefreshLayout = new WeakReference<PullToRefreshLayout>(new PullToRefreshLayout(this));

}

@Override
public void onDetachedFromWindow() {
    super.onDetachedFromWindow();
    try{
         MainActivity.wrefMPullToRefreshLayout.get().manuallyDetachFromWindow();
    }catch(NullPointerException e){

    }
}

Hope this helps.

Upvotes: 1

Related Questions