I created a class that inherits from SurfaceView. I would like to pass an argument to its constructor while calling findViewById(). Is it possible? How? How to handle it if not?
this.surfaceView = (TubeSurfaceView) findViewById(R.id.surfaceView);
Thank you.
Nope. findViewById is used on already instantiated views to find already instantiated children. Either you need to create the surface view programatically (in which case you wouldn't need to call findViewById), or you need to make whatever you want to pass via constructor an xml parameter.
Related
I have three items here: a base activity called ActivityName, a fragment that is called from that activity called FragmentName, and a dialogFragment that is called from the fragment.
I wanted to call a method from the dialog that resides in the fragment. I did this using the following:
((ActivityName) getActivity()).fragmentName.methodInFragment();
I had been trying a few different solutions and finally hit upon this one. I understand what's happening, but I don't understand what (ActivityName) is doing. My guess is that it's casting getActivity to the actual activity type so I can call things from it (like the instance of the fragment I have declared in it). Is that what's happening or am I way off?
Is this the same type of thing that's use when I am accessing a TextView I've defined in XML. e.g. TextView myTextView = (TextView) findViewById(R.id.thetextview);?
It is called cast. getActivity() returns an Activity object, and with ((ActivityName)getActivity()) you are specifying that getActivity() is actually an object of the class ActivityName. Casting to the specific type allows you to access the member and methods (public) that are not part of the super class. If you define a wrong cast, you will get a ClassCastException.
Is this the same type of thing that's use when I am accessing a
TextView I've defined in XML. e.g. TextView myTextView = (TextView)
findViewById(R.id.thetextview);?
yes it is. findViewById returns a View, and you tell that the View is actually a TextView. If instead of a TextView, R.id.thetextview, was the id of an ImageButton, casting it to TextView would have lead to a ClassCastException
Calling setHasOptionsMenu(true) from constructor, which is obviously called even before onCreate(), works perfectly! Can I do that? What will be problems?
Check here fragment - Android
Applications should generally not implement a constructor. The first
place application code can run where the fragment is ready to be used
is in onAttach(Activity), the point where the fragment is actually
associated with its activity. Some applications may also want to
implement onInflate(Activity, AttributeSet, Bundle) to retrieve
attributes from a layout resource, though should take care here
because this happens for the fragment is attached to its activity.
You may want to use another lifecycle event for this.
Yes, you can call setHasOptionsMenu(true) from the constructor.
I have an idea they both get the 'context' so that functions know how they fit into the scheme of things, is this right?
Any clues for the evolving ape?
this is a reference to the current class you are. It can be an Activity, Fragment, Adapter, View, etc. And when you use it what you are doing is just passing a reference of the current object of that class.
Let's say you are working on a custom View. Anywhere in the code of that view where you call this will be interpreted as the View itself, so the value of this changes depending on what class you are.
The method getActivity() is defined only in the Fragment class and any other class extending Fragment and this method returns and Object of the type Activity.
On Android development is very common mixing those two because most of the applications code is in Activity classes, and calling this on an Activity class will return an Activity object but as you can see they are not the same thing.
You can read more here
I have implemented a DialogFragment class which shows a dialog and call a custom listener when its button is pressed.
Since I want to reuse this class in many projects I need to pass the listener when I create the fragment. However I don't know how I could do it.
I know I should not pass the listener in the constructor because Android could call the default constructor if it has to recreate the fragment and I cannot pass the listener via Bundle arguments because Bundle does not support it.
The unique solution I found is make the activity implement the custom listener and check it in onAttach method. The problem is that this way would limit the implementing classes to the owner activity and maybe it does not fit to the application.
Does you find a better solution?
Thanks!
You normally have a static newInstance() method to create your fragment and pass in any values your fragment might need. That's the method to pass your listener to.
Have a look here where a newInstance() method is also used as an example:
http://developer.android.com/reference/android/app/Fragment.html
I am calling a class's member function from another class. So,I am creating an object of the class for which i have to call constructor. Within that constructor, I need to call onc reate method of my activity class.how can I do it?
Do not call onCreate from your constructor. The system itself will call onCreate for you.
Take a look at the Activity Lifecycle for more information of how onCreate is called.
Well,i presume u r trying to call a constructor in an activity.If that is the case then some thing is wrong in the way u have designed ur project.For more details check out these links
Creating an object of Android Activity class and Android - Activity Constructor vs onCreate
All method's of Activity class should be called within activity only . write your own methods for whatever operation you need to perform and pass data from other Activity/class by various data transfer algorithms .