How do I know when to use support classes?

I'm new to development for the Android platform and trying to figure out how to find out or predict when I need to use support classes? This is probably a stupid question, but let me give you a simple example. Let me say that I want to add sharing features to my application, so I go to the DAC and see an example there on how to do this. I carefully read the manual and saw a note about the limitations, i.e.

Note. ShareActionProvider is available starting at API level 14 and above

This seems to be fine for me since minSdkVersion has a value of 14 in my application. I copy-paste this fragment into my project, run the application and ... it crashes with the following error:

05-21 12:42:07.638: E/AndroidRuntime(25291): java.lang.UnsupportedOperationException: This is not supported, use MenuItemCompat.getActionProvider()

I am updating my sources to use the following code, and not the one I found in the documentation:

MenuItem menuItem = menu.findItem(R.id.action_share);
ShareActionProvider mShareActionProvider = (ShareActionProvider)MenuItemCompat.getActionProvider(menuItem);

Application Launch. At first glance, it works, but instead of the share icon, I can see the SHARE text in the ActionBar. Humm, after googling around I replace the next line in the menu layout

android:actionProviderClass="android.widget.ShareActionProvider" 

to

app:actionProviderClass="android.support.v7.widget.ShareActionProvider" 

Hooray! Finally I have what I want!

This is not the first time that I encounter such obstacles when learning the Android platform, so I'm trying to find if there is any rule of thumb that will help me predict such situations?

, , . ​​, Android Studio. Android Studio, , , SDK 21 22 . , Android. , Android ?

.

+4
1

, - , .

, " " , .

, , android:showAsAction="ifRoom" , - app:showAsAction="ifRoom". , backport. , appcompat-v7.

, , , , , , .

, Android.

. appcompat-v7 Android, API 1.

:

  • Android 1.x/2.x, backport , appcompat-v7 - backport, ( , ActionBarSherlock )

  • , Material Design, Android 4.4 , appcompat-v7

+1

Source: https://habr.com/ru/post/1589184/


All Articles