I am attempting to add the same action bar compatibility found in the Google "ActionBarCompat" sample code to one of my projects.
My problem is that I already extend MapActivity in my main class, but the main class of ActionBarCompat seems to require that you extend ActionBarActivity.
Here's my Java 101 question: I know enough about Java to know that I can't have multiple inheritance, but not enough to know the ideal way to handle this situation. My solution was to extend ActionBarActivity in my main class, but then make ActionBarActivity extend MapActivity. Is this the correct way to do this? If so, wouldn't I have to make a duplicate ActionBarActivity if I want it to extend other things?
Thanks for your consideration of this question!
Yes, that's a totally valid way of doing this. If you want an ActionBarActivity class that doesn't extend the MapActivity, you'll have to create another class. I don't think this will lead to a lot of code-duplication (which I'm guessing is what you're afraid of). Shame on the android developers though for making you need to code your own ActionBarHelper class.
Look at the example you provided - ActionBarActivity is only having an ActionBarHelper field and calling methods on it. Try doin the same in you MapActivity.
Use "Peter Knego"'s solution, and link to this library:
https://github.com/lvillani/android-compat-actionbar
or if your using Eclipse and svn :
https://svn.github.com/lvillani/android-compat-actionbar
Related
There is a class called CellSignalStrength that does not have a default constructor.
To be able to use SignalStrength, I have to make a class that extracts it, but I can't because when I try to do that, it prints out the error:
No default contstructor available for SignalStrength.
Also, there is another class called CellSignalStrengthGsm(same problem), but that class extends SignalStrength class, but how?
I've done some research on this, and i couldn't find anything, but this guide is only offering the signal strength of neighboring cell-sites to me, none of them is the one providing me with the signal. That solution is offering no actual signal strength.
Why doesn't the Android documentation have at least one example for how to use the class?
I really need help on this, I'm stuck :/
The only reason I can think of why you can't extend the CellSignalStrength class is because its constructor is protected CellSignalStrength(). Meaning only other classes in the same package may extend it.
The Android developers probably had good reason to do so. My suggestion would be to rethink what you're trying to do and figure out another solution. Perhaps using an instance of one of the classes that sub-classes CellSignalStrength:
Note: These classes are all final, so don't try to extend them.
CellSignalStrengthCdma
CellSignalStrengthGsm
CellSignalStrengthLte
CellSignalStrengthWcdma
I am working on a project on SoftKeyboard. I am editing the sample codes provided with the Eclipse ADT bundle. I realised it is already extends InputMethodService. However, I want to attach a ContextMenu in the soft key.
Therefore in this case, I need to extend Activity too. How do i solve this problem?
No, java does not support Multiple inheritance, Why because the child can't know which parent it has to choose if extends more than one class. To over come this you have to go for Interface.
For more info refer this, this and this
You can use Strategy pattern:
link link
You should create interface, then your classes should implemented this interface like in example in second link.
Java does not support multiple inheritance.
You can try this workarounds:
1)Aggregation: make a class that takes those two activities as fields.
2)Interfaces.
3)Rethink your design
public class MainActivity extends fragment
public class MainActivity extends Activity
The issue I have is that I need to extend Fragment to be able to use the 'pager_title_strip' but to use a list view that can read json, it also needs to be extended.
How can I extend both within the same class?
You can't, a class can only extend one other class.
It can however implement multiple interfaces, maybe that can help you in your design.
Abstractly spoken, multiple inheritance is not the only answer to such design problems, and in Java it was excluded by designers of Java (Gosling et.al.) because they saw it as inherently unsafe. Still in Java 8 multiple inheritance is only allowed for behaviour (see interface inheritance and new "default" methods), but not in state.
Alternatively, you can also think about composition or delegate patterns. From Joshua Bloch we know his recommendation "Favor composition for inheritance". I think this should be possible in your case.
There is FragmentActivity or ActionBarActivity class which may solve your problem!
http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html
The issue I have is that I need to extend Fragment to be able to use the 'pager_title_strip'
There is no requirement to use fragments at all with a ViewPager, let alone with a PagerTitleStrip or PagerTabStrip.
so saying that does this mean you aren't able to use a listview box that reads json and the pager title strip at the same time?
It is perfectly possible to "use a listview box that reads json and the pager title strip at the same time". However, you will need to create more than one Java class, in all likelihood, such as an Activity and a Fragment.
There are methods and attributes I would like to share across activities. E.g.
public class BaseActivity extends Activity
would be the parent for another activity
public class MainActivity extends BaseActivity
BUT if the child activity is a e.g. ListActivity this is not possible, right? Do I need a base class for ListActivity, too? This would be redundant code.
I could transform the ListActivity to an Activity, but this would be more code then necessary.
Any suggestions?
Yes, you're right. As java does not support multiple inheritance, a class can only have one base class.
But you can work via delegation instead of inheritance: Put your methods into a separate class (which does NOT inherit Activity) and use instances of it in your activities. Then you can reduce the redundant code (creating and holding the instance) to a minimum.
See also:
http://sourcemaking.com/refactoring/replace-inheritance-with-delegation
How do I implement multiple inheritence in Java
For the ListActivity case I would create a BaseListActivity that extends from BaseActivity. The BaseListActivity would contain a getListView() that returns the ListView if you really-really need it, a protected method setAdapter() that receives an Adapter/ListAdapter. You could also create some protected methods for enabling to show the empty view, to enable this behavior and to return the ids for your empty view and ListView. By default these should point to android.R.id.list/android.R.id.empty. But you can override that in your child activity.
In the end it's a matter of OOP and not that much of Android specific. If you check the implementation of ListActivity you'll see how simple it is to make your own BaseListActivity implementation. In the end it's more important to have a consistent and robust class hierarchy.
I am using my Activities this way, and I'm really happy I'm doing it. Of course you have to make some changes (like the ListActivity you mentioned in your question), but it has more advantages then disadvantages in my opinion.
In my app I don't have to worry about sending Analytics data every time I create a new Activity, and I also don't have to worry about setting up BroadcastReceivers I need in every class, I just made my BaseActivity abstract and have an abstract function what I call every time my BaseActivity gets a broadcast.
if you want to share some methods the best way it to impelement an interface or make an abstract class for baseactivty.
As others have pointed out, you need to use composition, since multiple inheritance is not possible in Java. This answer to a related question explains how to do this in the case of Android Activities.
The question doesn't really make sense, but this should help explain it better.
I'm currently using the IOIO, and it works like this:
public class MainActivity extends CustomIOIOActivity {}
From there I can call classes I've made in CustomIOIOActivity, which extends Activity.
The problem I'm running into now is that I have to inherit classes different from just Activity. For example, FragmentActivity.
Is there a way for me to create a custom IOIO activity which I can use for when I extend Activity and FragmentActivity? I don't want to have to make two essentially identical classes, which is what I am doing right now--seems silly. I'm not very familiar with Java, perhaps this is easy to do.
In Java you can't inherit class from more than one superclass. In order to achive your purpose, I would advise you to move all common logic to another class(IOIOHolder, for example), which would take an Activity parameter in constructor. Later you can create two different classes extending Activity and FragmentActivity, each containing an exemplar of IOIOHolder and using it for it's own tasks. By using this approach you can write IOIO code once and use it in any activity you want.