I am new to Android and I know that we can not extends multiple classes. Please tell me how to do that? Please Correct my code.
public class Tab1 extends Fragment,ListActivity {
}
You cannot extend multiple classes in Activity or Fragment,
Like this
public class MainActivity extends AppCompatActivity, baseClass{}
So what you can do is to create a new java BaseClass file that extends AppCompatActivity. The MainActivity will extent only BaseClass, Like this
public class MainActivity extends BaseClass{} //mainactivity extents baseclass
public class BaseClass extends AppCompatActivity{}//baseclass extents appcompatactivity
The advantage is that you can implement some base functions in the BaseClass, such as Toast, checking for internet connection, etc.
Please checkout the images in case of Activity.
MainActivity extents BaseClass,
BaseClass extents AppCompatActivity
In Case of Fragments
MyFragment extents BaseFragment,
BaseFragment extents Fragment
I hope it will be helpfull, Thank you.
The short answer is that you can't. Java doesn't allow for multiple inheritance. What you can do is implement multiple interfaces. So if you want to make a Fragment that has a ListView in it you can do something like this:
public class Tab1 extends Fragment implements BaseAdapter {
}
Unfortunately JAVA does not allowed you to extends multiple classes into one class. you can use Inner class or make your 2nd class a Interface you can include multiple interface but not multiple class. Inner class would be more suitable.
Related
I have a class myClass : UnityPlayerActivity() which derives from UnityPlayerActivity. This class is generated as following (and I'd rather not alter it)
public class UnityPlayerActivity extends Activity implements IUnityPlayerLifecycleEvents
I now need to call getSupportedFragmentManager but this is unavailable from a class which does not derive from AppCompatActivity. So I was wondering If I could someway get the SupportFragmentManager without changing the Unity generated class.
I'm new to android programming how to extend two activity . In my case I'm using ActionBarActivity
im already extends the a class for some functionalities how shall i extend two activities my class
any example code will be more useful for me
This is not much of a Android problem (as Activity is class as any other), it's the way Java works.
Java doesn't support multiple inheritance, so classes can only extend one other class.
class A extends B{
}
Even in this case:
class A {
}
class A extends another class - Object - but it is automatically implied without having to specify it.
If you want to ensure some functionality from several sources you will have to use interfaces and the implements keyword:
class A extends B implements C,D,E {
}
I would like to use RoboActivity with my activity, but I don't know how to do that coz my current activity extends already ActionBarActivity:
public class MainActivity extends ActionBarActivity
Thank you so much
For RoboGuice 3 simply extend from RoboActionBarActivity.
You can simply copy the relevant RoboActivity parts into your subclass of ActionBarActivity. From the official docs https://github.com/roboguice/roboguice/wiki/Using-your-own-BaseActivity-with-RoboGuice:
In case your base activity class already extends a different library, the best way to get all the power of RoboGuice is to copy all the code contained in RoboActivity.
An example is also provided therein.
I just want to know if I am supposed to extend every class with the following:
public class [name goes here] extends Activity
first, let me explain. below you will see an example of 4 classes I have in my project (of so many!). The Main class of course has to extend Activity. Well, within the main class are buttons. One button is for the location. I have Location extends Activity. Now, should this attend Activity, or should it extend Main? What about LocationA? Should this extend Location or Main?
public class Main extends Activity{
public class Location extends Activity{
public class LocationA extends Activity{
public class LocationB extends Activity{
Thanks for ALL of your help!
Unless you want to share a common functionality between all your activities, All your activities should extend Activity class
I have created an abstract class which extends the Activity Class.
I want to extend it in an activity which extends TabActivity Class, how can I achieve that?
Get the TabActivity and abstract class Functionality at the same time.
I do realize that multiple inheritance is not possible in Java, but can I avoid extending TabActivity Class and still use getTabHost()?
10x :)
I don't think you can achieve this with this design. If your abstract class extending SomeOtherActivityClass and new Class need to extended SomeotherActivityClass (through inheritence) and TabActivityClass, that may confuse dalvik to choose which activity at runtime and it doesn't make sense. You may need to reconsider your design.
yes sure, you can use. By using following snippet:
TabHost host=(TabHost)findViewById(id of tabhost);
It's better way to create another class that extends Application class. Or use singleton pattern and create class with all functionality that you're need.