Friday, July 24, 2015

Setting up Global Hotkeys in Spotify on Linux Mint (Xfce)

This isn't really a coding post but I feel like it is kind of relevant since, while I'm coding, music is a big part of my day. I use Linux Mint (Xfce) on my workstation and despite the negative press that Spotify gets about various issues, I remain a fan of their service. A while back I started using the Spotify Linux Preview Client on my Linux machine and I liked it quite a lot. However, it was lacking something that I really missed... global hotkeys. So, I did a little research and these are the steps that I followed in order to achieve this most useful feature.


  1. You can get the Spotify Linux Client by following the directions shown here: https://www.spotify.com/us/download/linux/
  2. Download and install the latest playerctl release (deb file) from this page: https://github.com/acrisci/playerctl/releases
  3. Disable Xfce global hotkeys that you plan on reassigning to Spotify in this file: xfce4-keyboard-shortcuts.xml (It is in multiple locations. You may have to tinker with this.) The one that worked for me was located at /usr/share/mint-configuration-xfce/xfce4/xfconf/xfce-perchannel-xml
  4. Comment out any entries dealing with control+alt+home, control+alt+left, and control+alt+right
  5. In the Xfce keyboard shortcuts add the following shortcuts
    1. control+alt+home -> playerctl play-pause
    2. control+alt+left -> playerctl previous
    3. control+alt+right -> playerctl next

Of course feel free to customize the keys any way you like. The above is just my setup. Again, the above steps are for Xfce only.

Monday, July 20, 2015

Can not perform this action after onSaveInstanceState

Today I was trying to do something that would, on the surface, seem very simple. All I needed to do was show a custom dialog upon the user pressing a button. The dialog was designed to gather some additional required information before processing the user's request. This was simple enough to implement using the DialogFragment class. However, after the screen orientation changed, pressing the button would immediately cause an "Unfortunately <app> has stopped" message. I checked the stack trace and found the error message referenced in the title of this post. I tried many many solutions that I found across the net but in the end this is what did it for me.

Android has a built in generic class called WeakReference. I used this class to store a reference to the parent activity (the one tasked with showing the dialog). Once I created this reference and used it to retrieve a reference to the fragment manager everything worked perfectly regardless of how often I rotated the screen!

Example Code:

I decided to try this approach in the hopes that WeakReference would let go of all references to the old ParentActivity in the event of a screen orientation change. I didn't expect it to work, but after trying everything else I figure it wouldn't hurt to give it a shot.

I'll be honest, I'm not totally sure why this works. I know that the ParentActivity is destroyed and recreated every time the screen is rotated, but the ChildDialog isn't retaining any kind of references to the parent that I know of. The only thing that is being referenced from the WeakReference is the FragmentManager of the ParentActivity. If you have further details on why this approach is working so well, please feel free to comment below.