sending data from activity to a method implemented in a fragment - android-activity

I have read through many posts on here with sending data from activity to fragment or should i say invoking a method in main activity that was implemented in fragment class like whhat i have described below. but im looking for another way other than implementing interfaces or putting code in onCreate of the activity or onCreateview of the fragment.
I have this function in a fragment class
public void speaker(String msg){
tts.speak(msg, TextToSpeech.QUEUE_FLUSH, null);
}
Is it possible to call this function anywhere in the main activity to pass a string value to the function like speaker(text); and how do is this accomplish other than interfaces or putting code in onCreate or onCreateview??

Related

Run function with Toast in Application Class

Thank you all so much! I just started in Kotlin which probably should be called the K language (like C and F), and have found so many solutions here on this site...it's awesome!
I have an independent class file called AppTime.kt and it's declared in the AndroidManifest.xml file:
<application
android:name=".AppTime"
class AppTime : Application() {
fun burntToast(sMsg: String) {
Toast.makeText(this.applicationContext, "!", Toast.LENGTH_LONG).show()
}
}
It doesn't run when called anywhere from a Fragment class:
class FirstFragment : Fragment() {...
AppTime().burntToast()
I've tried every approach using parameters for the Toast following makeText(...
and then to call it from a Fragment with or without context or string parameters.
Is it the type of class I have?
Functions defined inside a class can only be called on an instance of that class, as you already found.
But you cannot simply instantiate an arbitrary Application and expect it to work. Android does a lot of behind-the-scenes setup of framework classes before they are usable. Any Application or Activity that you instantiate yourself is useless. You have to use the instances that are provided to you through the lifecycle of the Activities that get launched in your application.
If you want to call this function from your Fragment, you will have to get an instance of your application, which you can get from its associated Activity. Since the Activity class doesn't know about your specific subclass of Application, you must also cast the application to your specific subclass to be able to call its unique functions. You can get the Activity by using requireActivity().
(requireActivity().application as AppTime).burntToast()

Angular2 redirect from static method

Hi i have 'static class' Utils with only static methods (helpers):
export class Utils {
static doSomethingAndRedirect() {
...doo something...
redirectTo->'/home'
}
}
So how redirectTo code should look like?
You have a wrong approach to the usecase you are trying to solve. Have a look at ngrx/store and ngrx/effects.
In short, you define actions and reducers which modify the state of your app. Next, you can react to different actions with different side-effects (ngrx/effects), for example in my app I have got:
Actions: LoginAction and LoginSuccessAction
Effects: when LoginSuccessAction is triggered, my effect redirects to /dashboard component
This makes for nice separation of concerns:
views display the current state and dispatch actions that change the state
actions specify what happens in the app
reducers specify what changes to state occur for different actions
effects specify what side-effects occur for certain actions
The best solution that I found in my case was just... add parameter 'router' into static function argument list:
static doSomethingAndRedirect(router) {
...doo something...
router.navigateByUrl('home'); // redirect
}
This is a kind of compromise between static helper convenience and non-static "angular way".

Having difficulties extending Zend_Form

Currently I have big difficulties extending Zend_Form.
I have the basic class called Forms_LpaManageEmailForm.
It is used separately and works fine.
Next I've created a new class form
called Default_Form_CartReport witch extends Forms_LpaManageEmailForm.
So the task is to render Default_Form_CartReport and slitely modificate it.
In other words I need all functionality of
Forms_LpaManageEmailForm class but with overriden _addMultiOptionsForMultiSelect() function
(what is done) and changed button label (doesn't solved).
In basic class I have hidden element named id which value is filled with
$this->_entry_id['entry_id']. When I use basic form separately - its woks fine. But
when I run extended form(Forms_LpaManageEmailForm) I see that hidden id element's value is empty. In basic class in construct section I run
Zend debugger(with this line Zend_Debug::dump($this->_entry_id['entry_id'])) to see if the
value is passed. And it's passed :) When I repeat this in init() section it shows NULL...
As I barely understand - the problem lays in init() functions, in the way it is called.
I think something is wrong with Default_Form_CartReport class skeleton.
I've uploaded code to: PASTEBIN
Really need help in this question.
Thank you!
I believe your issues are causing my the fact that Forms_LpaManageEmailForm:: __construct is calling $this->init() directly. if you open the Zend_Form, you will notice that the __construct is also calling the $this->init() function. This cause your init() function to executed twice.
Try to load all your logic & elements solely in the __construct function, and don't use the init() function. also, the __construct function in each form class should always call the parent::__construct before any additional logic.

DelegateCommand<object> test with EventArg parameter mstest

I currently have an event trigger firing a custom trigger action.
The action passes back a EventArgs type of object to the view's view-model.
This is all well and good when I run the code it works perfectly. However, when I come to test this portion of code it all goes a bit rubbish.
As stated We are using an MVVM type pattern so I'm testing the 'Doing' end of the event trigger in my view-model and what I want to do is create a 'mocked' EventArgs object to pass into the execute method of my command under test. However it requires a RoutedEvent as it's ID property as stated above and I don't have access to it's constructor!
Cannot Access Internal Constructor for 'RoutedEvent' here.
Has anyone got any ideas? The code converage in test is more important than the current implimentation so if this is thought to be 'untestable', then I can make changes.
I have answered my own Question I think.
Casting the object passed back from the view at an earlier point means that the object I am passing to the methods under test is more easily created.
This is what I have now for the method under test.
public void DoItemsChanged(IList param)
Before I had
public void DoItemsChanged(object param)
Where the param is a SelectedItemCollection (previously a RoutedEventArgs, but now I use the IvokeCommandAction on the event trigger in the view, passign the SelectedItems). The param is now more easily passed into the method for the test and the code it much more descriptive as well. So it's all good for everyone.

Can I register a custom model binder somewhere other than Global.asax?

It would be handy to limit the scope of a custom model binder for just a specific controller action method or its entire controller. Hanselman wrote a sentence that implied alternative locations for custom model binder registration but never seemed to finish the thought:
You can either put this Custom Model Binder in charge of all your DateTimes by registering it in the Global.asax
Is it possible to make these registrations at a smaller scope of the controller system? If so, is there any reason to avoid doing so outside of the Global.asax MvcApplication (e.g., performance reasons)?
As I was closing the tabs I opened for this question that I hadn't reached before giving up, I found someone with an answer. You can assign a ModelBinderAttribute to your view models:
[ModelBinder(typeof(SomeEditorModelModelBinder))]
public class SomeEditorModel {
// display model goes here
}
public class SomeEditorModelModelBinder : DefaultModelBinder {
// custom model binder for said model goes here
}
While it wasn't quite what I was looking for, it is even more specific than registering it for a controller or controller method.
Update
Thanks to Levi's comment pointing out a much better solution. If you are consuming the object with a custom model binder in an MVC action method directly, you can simply decorate that method's parameter with the ModelBinder property.
public ActionResult SomeMethod([ModelBinder(typeof(SomeEditorModelBinder))]SomeEditorModel model) { ... }