Pages

20 Feb 2014

Alternative to Intent.FLAG_ACTIVITY_CLEAR_TASK

I want to use Intent.FLAG_ACTIVITY_CLEAR_TASK to start my app from a notification on Android's status bar like it's restarted, but as you may know, Intent.FLAG_ACTIVITY_CLEAR_TASK requires Android API 11, so I found an alternative for it. Basically, just replace:

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(this, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);

with

ComponentName cn = intent.getComponent();
Intent restartIntent = IntentCompat.makeRestartActivityTask(cn);
PendingIntent contentIntent = PendingIntent.getActivity(this, REQUEST_CODE, restartIntent, PendingIntent.FLAG_UPDATE_CURRENT);

And it works as expected ;)

5 Feb 2014

A hack to catch soft keyboard show/hide event on Android

Today at work, I got this problem: catching event when soft keyboard comes up/down (or show/hide) ?!

At first, I thought it would be easy to resolve, but after a while of googling, I realized that there is no official event or listener in Android API for that, but I did find some workarounds like:
http://stackoverflow.com/questions/4312319/howto-capture-the-virtual-keyboard-show-hide-event-in-android
http://stackoverflow.com/questions/3793093/android-edittext-soft-keyboard-show-hide-event

Well, there are some workarounds that were already posted but they are incomplete (some work on 4.0.4 but not 2.3, some don't work all the time), but at least they are still useful, they gave me an idea to make this "complete" workaround (well, at least it works like a charm in my case for now, I tested on both Android 2.3 and 4.0.4), so I would like to share it here, hope it helps someone out there ;)

Usage: just use following LinearLayout overridden class as the main layout(and of course, use "match_parent" for its layout_width and layout_height) of the activity where you want to catch soft keyboard show/hide events, then setOnSoftKeyboardVisibilityChangeListener for your main layout. Finally, you need this line in your <activity> tag (in manifest.xml) where SoftKeyboardHandledLinearLayout is used:

android:windowSoftInputMode="adjustResize"

Take a look at following code to get a better idea of how to implement this hack.