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
Related
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.
the clip of the code written for using voice interaction which is used in another Java file I have already tried importing android.app.VoiceInteractor but its not working
You may have imported all necessary classes in your code. As your snippet you forgot to extend that, But you are already extending AppCompatActivity so that you can't extend another class in the same Activity. What you need to do is create another class inside MyVoiceActivity and extend VoiceInteractor.ConfirmationRequest in that class.
Your Activity will look something like this
public class MyVoiceActivity extends AppCompatActivity{
class Confirm extends VoiceInteractor.ConfirmationRequest {
//All method you wanted goes here
}
}
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.
This question already has answers here:
Can I use implements and extends at the same time?
(3 answers)
Closed 2 years ago.
So i have public class GPSTracker extends Activity { .. } and after that public final class GPSTracker implements LocationListener . the second one i found in a tutorial, the whole location tracking process happens here, but as far as i understand, i need to include onCreateOptionsMenu and onCreate methods in order to make it work. The question is; How do i put these two together?
here is the full code, so that you can analyze better.
Any help would be much appreciated. Thanks in advance.
Cou can combine them as following:
public class GPSTracker extends Activity implements LocationListener {
}
and put all your code of both classes into this one. The onCreatOptionsMenu is only necessary if you're using a menu. The onCreate should be used because your GPSTracker class is an Activity.
You can not have two public classes in same file but you may include another public class within one.
public class A{
public class B{
//some code
}
}
OR
You can have following implementation.
public class GPSTracker extends Activity implements LocationListener{}
I am developing an framework which is used overall the application, Which extends an activity, But in some scenario am using maps in my project, So i need to extend MapActivity,I can't Activity for example,
Framework Level,
Activity extends CommonActivity extends MainActivity
Here is my problem,
If i extends MapActivity instead of Activity it causes any problem or it has any drawbacks?
Thanks,
Nikhilreddy.
I don't think so there are any drawbacks of map activity because map activity extends Activity Class.
public abstract class MapActivity extends android.app.Activity
But You have to Run your application on a device or emulator that has Google Maps installed.