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
}
}
Related
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.
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 have to do capture Applaunch and App minimize events for myapp for Appirater integration. I have to show Appirater dialog for every 3rd app launch/app minimize
I followed the article
http://vardhan-justlikethat.blogspot.in/2013/05/android-solution-to-detect-when-android.html
But i have a problem, i have many activities,fragments in Application.say some Activities extends FragmentActivity, some of the them are fragments and some of them extends Activity
But, as per the article, i have to extend all the activities using Base Activity
It implies the architecture changes for my application.
If i write the code separate for each activities, the Appirater wonk work proper. Any alternatives to this ? please help in fixing
Tried also by extending Application class but it can capture app minimze events
Create a BaseActivity And a BaseFragmentActivity that does the same as the BaseActivity in the example, and then extend them in the appropriate places, should still work the same and you don't have to do much work. Correct me if I'm wrong, I just skimmed through it
Edit, example:
public class BaseActivity extends Activity{
//everything
}
and the other
public class BaseFragmentActivity extends FragmentActivity{
//everything
}
I am making an app in which i have to run an activity in background until the app is running.each activity related to this app is using first activity.how can it possible?
can i use the inheritance for this?
can anyone tell me any example of multilevel interitance in android?
You can create a BaseActivity class that extends Activity and all other Activities will extend this BaseActivity. Then what ever happened in all other activities (like resume and pause) will also effect the actions of BaseActivity.
If you have to accomplish background task you better to see android service
You are already extending the Activity class in a base class, and again you are extending this base class in other class. This is itself an example of multilevel inheritance. I am posting an example that may be relevant for your question:
public class basecls extends Activity{
/*The base class*/
}
public class secondcls extends basecls{
/* basecls extended by secondcls */
}
You can extend the secondcls in another class, and the new class will inherit all super classes such that you can use the methods of its super classes.
Your main activity is already using inheritance, since it extends the Activity class.
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.