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.
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'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 have two different class(class A and Class B). i want to use the method of Class A in Class B. i normally used object for class A and called method in class B. but unfortunately i am getting Force close error. Is that any thing different to call a method of another class in android. I referred many articles in stackoverflow. but i cant understand properly. pls help me to find out the solution.
You should not create object like this, you should use context to call object like as below
((Class A) contextObject).function();
it runs perfectly on my system,
earlier class A and class B both are extending Activity and now only A extends Activity and B extends A and now B can call functions of A
this works for me:-
public class A extends Activity
{
functionOfA(){}
}
public class B extends A
{
//calling function of class A
functionOfA();
}
In case of Android, class which extends Activity will maintain its life cycle methods. if method which is defined in different class other current running activity may be killed or in pause state. so it is suggested that if method which is reusable in application should in different class for example (AppManager singleton class) rather than being in single activity class
u have to create constructor of class A
& in class B make an obj to class A
initialize it with
ClassA obj=new classA();
obj.method_A();
hope this will help
As I said in my comment, you shouldnt instantiate activities.
If your method uses some method that are called from a Context object, you can create a new class ( Class NewClass) that accepts a context parameter and implements your methods in it.
So this way, you can call your class from any activity:
NewClass nc = new NewClass(this);
Look up for some example of how to use a database in Android. It uses the same way.