I have this android application and I am trying to extend two classes at the same time. I have this code:
public class TimelineFragment extends Fragment {
public class TimelineFragment extends Activity {
//all codes here
}
}
On my second TimelineFragment, it has an error that says: The nested type TimelineFragment cannot hide an enclosing type
I have this android application and I am trying to extend two classes at the same time
That is not possible. Java does not support multiple inheritance.
I have this code
Given your class name is TimelineFragment, one presumes that it should extend Fragment. Whatever problem you are trying to solve via multiple inheritance will need to be solved in some other way. For example, if you are trying to perform operations on the activity that hosts your fragment, you can call getActivity() from the fragment.
Multiple inharitance is not possible in Java, and therefore in Android.
multiple inheritance is not possible in java so therefore u cant't use it in android as well.
Well one thing you can do is to create interfaces instead of class and implement any no. of interfaces you want and their methods.
Related
Hi I have this one Activity, and I need to send data from it to two different fragments. Is there a way to set this up?
public class MainActivity extends AppCompatActivity implements{ FragmentOne.OnEventListener, fragmentTwo.OnEventListener {
private static String TAG = MainActivity.class.getSimpleName();
So basically I am trying to use one interface and I have all the other methods set up in my two fragments and everything works as intended for fragmentOne. I am just trying to figure out how to make the same listener and interface work for the second interface as well. Is there a way to make it work?
Thanks in advance!
As you wrote, "the same listener and interface", so you don't need to hold two instances of the interface, but just make the mainActivity implements the same one. (if it's not the case post your interface and fragments code).
I assume you are semi-following the android fragment tutorial? Please share the rest of your code.
It should work as long as your activity is correctly implementing both interfaces. Java cannot extend multiple classes but can implement multiple interfaces. However your activity must provide implementation for both of the onEventListeners.
I cannot decide what approach should I use in the next situtation.
One activity from my app need to have different functionality, here is the leak of multiple inheritence comes into play.
What I need to get ?
There are several parts that my activity has to have.
Navigation drawer. I am using MaterialDrawer library. It requires activity to implement on click callbacks (or use composition instead), but also it use activity as constructor argument,so I think it will be better to put this into separate class inherited from Activity or any base class provided by Android Framework . Thanks to library developer it doesn't require any stuff to be done in on create method ( setContentLayout for instance)
My activity(one of several) will have only Toolbar and FrameLayout for holding fragment . Or it is better to separate toolbar and single fragment ?
This in turn requires some stuff to be done in onCreate : setContentLayout with basic layout , add fragment to the container set activity actionbar .....
Maybe in future I will use another libraries that requires to add something in activity lifecycle methods.
All these points in order to follow Single Responsibility principle have to be separate classes inherited from some base Activity.
For example we will have something like this.
public class SingleFragmentActivity<T extends Fragment> extends AppCompatActivity {
}
public class SingleFragmentToolbarActivity<T extends Fragment> extends AppCompatActivity {
}
public class NavigationDrawerActivity extends AppCompatActivity {
}
....
As you can see each functionality is put into the separate class, but what if I need to have SingleFramgentActivity with NavigationDrawer ?
In this case I have to inherit one of these classes from another, for example
public class NavigationDrawer extends SingleFragmentActivity {
}
If to do so, I have no ability to use navigation drawer separately from SingleFragmentActivity.
What is the best practice to follow in this case, how to build class hierarchy to make it flexible and use Open-Close principle, to make changes in application without any pain. (Use strategy, decorator ..... ?)
I would be grateful everyone for help.
Is there a way to this?
I want to create a fragment, that either extends the support fragment or the default fragment... I think this is not possible, so I ask the question in another way: what's the most beautiful workaround you know/use?
I think this is not possible
Correct.
what's the most beautiful workaround you know/use?
Put the business logic in a separate class (I will call it FragmentHelper). Create one fragment class that extends the native Fragment implementation, and have it delegate work to the FragmentHelper. Create another fragment class that extends the support package's Fragment implementation, and have it delegate work to the FragmentHelper.
I have a parent Activity that ALL other activities in my app extend from, let's call it MainActivity. At first it was extending OrmLiteBaseActivity, like so :
public class MainActivity extends OrmLiteBaseActivity<DatabaseHelper>
Now that I want to use ActionBarSherlock in my app, I have to extend from SherlockActivity... But I can't extend from two classes at once.
Has anybody worked with ORMLite and ActionBarSherlock before ?
As said by PratikButani: I ended up using ORMLite's OpenHelperManager for each time I wanted to used the database.
Link : https://stackoverflow.com/a/7662537/1318946
My project consists of couple of activity and ListActivity items, there is some common piece of code(Navigation bar and some other codes) which needs to be done on both type of activity.
Is there a way I extend the activity and write my piece of code, and let ListActivity also inherent that code ?
right now I am copying the same piece of code in two classes , one is Activity extended and other is ListActivity extended
You could also have the common code in a class CSuperCommon, and have each of your Activities contain an inner class that inherits from CSuperCommon. Some initialization will need to be done such as setting the parent view, context, etc.
There is no real multi inheritance in java (and so in android) but it is possible to simulate it: http://www.javaworld.com/javaworld/jw-10-2005/jw-1024-multiple.html.
Here is another answer from stackoverflow: How do Java Interfaces simulate multiple inheritance?
Have both extend a Base Activity class (which will have your common code) and implement a list view in one of them. Implementing a list view is very easy!