Search code examples
androidandroid-tabhostgoogle-cloud-messagingandroid-2.3-gingerbread

Android: Can GCM and Tabhost coexist without fragments?


I have a project that requires both GCM and Tabhost, but declaring them both in the same MainActivity class causes the application to crash and I cannot use fragments as my application needs to be backwards compatible with Gingerbread. Does anyone know of a workaround?

Edit: Well, I found this nice tutorial for implementing Fragmented Tabs in pre-Honeycomb versions, but I'm still experiencing the same issue. http://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/

How can I implement GCM in such a way that it doesn't interfere with Tabs/vice versa? What am I missing?


Solution

  • GCM and TabHost seem to play nicely after all. The root of the problem turned out to be neither, but rather an Options menu. If anyone is setting up GCM using the DemoActivity Library provided by Google check for this nasty piece of code before you tear your hair out:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
       switch(item.getItemId()) {
           case R.id.options_register:
               GCMRegistrar.register(this, SENDER_ID);
               return true;
           case R.id.options_unregister:
               GCMRegistrar.unregister(this);
               return true;
           case R.id.options_clear:
               mDisplay.setText(null);
               return true;
           case R.id.options_exit:
               finish();
               return true;
           default:
               return super.onOptionsItemSelected(item);
       }
    }    
    

    I'm not smart enough to figure out why this is problematic, but it does not like TabHost or Tabs created with Fragments (I tried both).