0

I am trying to overlay the actionbar with a TextView everytime the user pulls down to refresh the list view that is displayed (exactly the way the Gmail app or the reddit sync app do it). I am using a SwipeRefreshLayout for the refreshing, but I guess that's irrelevant. I have a class (HeaderTransformer) that holds the TextView and uses a WindowManager to attach the textView to the display. The instance of the HeaderTransformer class is called from within a fragment (which holds the SwipeRefreshLayout and the ListView to refresh). On the very first creation of the fragment, the TextView overlay works perfectly. However, if I rotate the screen or somehow recreate the fragment, the WindowManager does not attach the TextView and raises a BadTokenException.

Here is some code:

Fragment Code:

private HeaderTransformer ht;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
   Bundle savedInstanceState) {
   // Inflate the layout for this fragment
   View view =  inflater.inflate(R.layout.fragment_misc, container,     false);
   View refresh_header = getActivity().getLayoutInflater().inflate(R.layout.swipe_refresh_header_layout, null);
   ht = new HeaderTransformer(getActivity(), refresh_header);
}
@Override
public void onResume() {
    super.onResume();
    getActivity().runOnUiThread(new Runnable(){
       @Override
       public void run() {
       if(!getActivity().isFinishing())
               ht.addHeaderViewToActivity(getActivity());
       }
       });
}

HeaderTransformer.java

public class HeaderTransformer {
    private View mHeaderView;
    private TextView mMainTextView;
    private Context mContext;
    public HeaderTransformer(Context context, View headerView) {
        mHeaderView = headerView;
        mContext = context;
        mMainTextView = (TextView) headerView.findViewById(R.id.ptr_text);
    }

    public void onReset() {
        mMainTextView.setVisibility(View.VISIBLE);
        mMainTextView.setText(R.string.pull_to_refresh_label);
    }

    public void onRefreshStarted() {
        mMainTextView.setText(R.string.refreshing_label);
    }

    public void addHeaderViewToActivity(Activity mActivity) {

        Rect mRect = new Rect();
        // Get the Display Rect of the Decor View
           mActivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(mRect);

        // Honour the requested layout params
        int width = WindowManager.LayoutParams.MATCH_PARENT;
        int height = WindowManager.LayoutParams.WRAP_CONTENT;
        ViewGroup.LayoutParams requestedLp = mHeaderView.getLayoutParams();
        if (requestedLp != null) {
            width = requestedLp.width;
            height = requestedLp.height;
        }

        // Create LayoutParams for adding the View as a panel
        WindowManager.LayoutParams wlp = new WindowManager.LayoutParams(width, height,
            WindowManager.LayoutParams.TYPE_APPLICATION_PANEL,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
                    WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
            PixelFormat.TRANSLUCENT);
        wlp.x = 0;
        wlp.y = mRect.top;
        wlp.gravity = Gravity.TOP | Gravity.LEFT;
        // Workaround for Issue #182
        mHeaderView.setTag(wlp);
        try{
            mActivity.getWindowManager().addView(mHeaderView, wlp);
        }
        catch(Exception e){
            e.printStackTrace();
        }
}

Here's the stacktrace:

05-14 00:09:03.286: W/System.err(18190): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running? 
05-14 00:09:03.286: W/System.err(18190): at android.view.ViewRootImpl.setView(ViewRootImpl.java)
05-14 00:09:03.286: W/System.err(18190): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java) 
05-14 00:09:03.286: W/System.err(18190): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java) 
05-14 00:09:03.286: W/System.err(18190): at com.faisalalqadi.resume.util.HeaderTransformer.addHeaderViewToActivity(HeaderTransformer.java:72) 
05-14 00:09:03.286: W/System.err(18190):at com.faisalalqadi.resume.fragment.MiscellaneousFragment$3.run(MiscellaneousFragment.java:260) 
05-14 00:09:03.286: W/System.err(18190): at android.app.Activity.runOnUiThread(Activity.java) 
05-14 00:09:03.286: W/System.err(18190): at com.faisalalqadi.resume.fragment.MiscellaneousFragment.onResume(MiscellaneousFragment.java:256) 
05-14 00:09:03.296: W/System.err(18190): at android.support.v4.app.Fragment.performResume(Fragment.java:1543) 
05-14 00:09:03.296: W/System.err(18190): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:974) 
05-14 00:09:03.296: W/System.err(18190): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115) 
05-14 00:09:03.296: W/System.err(18190): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1097) 
05-14 00:09:03.296: W/System.err(18190): at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:1905) 
05-14 00:09:03.296: W/System.err(18190): at android.support.v4.app.FragmentActivity.onResumeFragments(FragmentActivity.java:466) 
05-14 00:09:03.296: W/System.err(18190): at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:455) 
05-14 00:09:03.296: W/System.err(18190): at android.app.Activity.performResume(Activity.java) 
05-14 00:09:03.296: W/System.err(18190): at android.app.ActivityThread.performResumeActivity(ActivityThread.java) 
05-14 00:09:03.296: W/System.err(18190): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java) 
05-14 00:09:03.296: W/System.err(18190): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java) 
05-14 00:09:03.296: W/System.err(18190): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java) 
05-14 00:09:03.296: W/System.err(18190): at android.app.ActivityThread.access$900(ActivityThread.java) 
05-14 00:09:03.296: W/System.err(18190): at android.app.ActivityThread$H.handleMessage(ActivityThread.java) 
05-14 00:09:03.296: W/System.err(18190): at android.os.Handler.dispatchMessage(Handler.java) 
05-14 00:09:03.296: W/System.err(18190): at android.os.Looper.loop(Looper.java) 
05-14 00:09:03.296: W/System.err(18190): at android.app.ActivityThread.main(ActivityThread.java) 
05-14 00:09:03.296: W/System.err(18190): at java.lang.reflect.Method.invokeNative(Native Method) 
05-14 00:09:03.296: W/System.err(18190): at java.lang.reflect.Method.invoke(Method.java) 
05-14 00:09:03.296: W/System.err(18190): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)

I've been looking all over for anything to indicate anything out of the ordinary. All the resources I've looked at have said to swap getApplicationContext() for Activity.this. I think the source of the error is that HeaderTransformer is holding a reference to the activity/view that was destroyed/removed on orientation change. I attempted removing any reference to the view or activity being held by the instance of the HeaderTransformer instance (and changed all the functions to be passed mHeaderView as a paramater (instead of a local variable). I still get the BadTokenException.

Does anyone know what could be causing this?

Vamshi
  • 1,495
  • 1
  • 15
  • 31
  • http://stackoverflow.com/questions/4187673/problems-creating-a-popup-window-in-android-activity see this link – duggu May 14 '14 at 04:46

1 Answers1

0

So save your instance and when rotate screen get your saved instance and pass to transform class.

Jayesh Khasatiya
  • 2,140
  • 1
  • 14
  • 15