📜 ⬆️ ⬇️

Revise old projects for Android 4.0. Part two

Most recently, I published a topic that talked about code that worked in Android 2.3, but stopped working in Android 4.0. Several people wrote in person to the post office, thanked for the information and asked to continue such publications. This time let's talk about ActionBar. One user had a problem with the code that worked on Android 3.0, but again stopped working on Android 4.0.

A small digression - Evil you, I'll leave you


About 15 years ago, when I began to independently study programming and simultaneously connected to the Internet, I noticed an amazing thing. When I, as a newcomer, asked a question on a Russian-speaking forum, I received silence in response, or I would advise me to teach a materiel and not get under my feet. When I asked a similar question on the bourgeois forums, and even in bad English (I studied German at school), I received detailed answers. Since then, almost nothing has changed. I have long since scored on our forums and prefer to communicate only “there”. Surely, many noticed how strikingly different comments on blogs. They have: nice tutorial, thanks for post, excelent article. We have: why another bike, your horizon is littered, the colors of the eyebrow, vindekapets, etc.
I personally know several of the “former” who blogged in two languages ​​- English and Russian, and then left only the English part. Do you understand why?
It was the same with my previous post. Here are typical statements: shoot, severe Chelyabinsk programmers, etc. Somehow it coincided that on foreign resources such questions came up several times, but no one offered to shoot for the wrong code, but simply explained why it was impossible to do that. One example is http://stackoverflow.com/questions/9442527/networkonmainthreadexception-when-reading-from-web . In general, only this resource yielded 90 results on the request of NetworkOnMainThreadException . And no one swears. As they say, feel the difference. Mentality that we have such.

New problem - the icon does not work in ActionBar


This problem was shared by one of the registered users of my site . The bottom line was the following: The application icon can work as a navigation element and play the role of the Home button, like the Habra logo on the site in particular. This is very convenient in some cases when an application consists of several screens and you can “poke” into the system application icon for a quick return. The example is described in the documentation and worked fine on a tablet running Android 3.0, and suddenly stopped working under Android 4.0.
In principle, the answer on the Internet was found quickly, moreover, even in the documentation you can find a solution. But, apparently, the person was just tired or the eye was crawling, but the applicant did not find it. And the casket opened simply. For the application icon to work as a navigation element, you must explicitly set the setHomeButtonEnabled () method

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.two); ActionBar bar = getActionBar(); bar.setHomeButtonEnabled(true); } 

It turned out that in Android 3.0 the icon was enabled as the default navigation button, but not in Android 4.0.
By the way, I found an interesting moment associated with this button. The system allows you to add an extra arrow to the left of the application icon using the bar.setDisplayHomeAsUpEnabled (true) method ;
')


So, if you call this method, then the previous method setHomeButtonEnabled () is not necessary to call, everything works.
Happy programming!

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


All Articles