Creating a helper class in Android for storing variables - android-activity

I'm making a music app, which has a lot of variables flying around as songs are chosen, based on artists & albums and all sorts of things.
I'm trying to make my app a bit easier to use by sending all of those variables out to a new class, which I consider as kind of like a 'helper cell' in excel.. It stores a bunch of data so I don't have to keep passing and receiving extras.
The idea is this:
The user chooses a song, and the artist/album/song information is passed out to the helper class.
Next, a music service starts, taking the chosen songs path from the helper class, and playing it.
At the same time, an activity starts, displaying the chosen artist/album/song, again, from the helper class. Next/previous buttons are included here, and once clicked, the helper class' current song is changed, and the music service is instructed to receive the variables from the helper class again.
What type of class would this be? What would it extent, how can I instantiate it, and how can I send and receive variables to/from that class?

Use an Application instance to hold application-level variables.
Extend android.app.application
Define your application in the AndroidManifest.xml
<application android:icon="#drawable/ic_launcher" android:label="#string/app_name" android:name=".MainApplication">
To get your application instance in your activities, call getApplication()
For example, in your onCreate() method of your activity, you would use this code, assuming you named your application class "MainApplication":
Application application = ((MainApplication)getApplication());
Album album = application.getAlbum();
Song song = application.getSong();
...

Depending on what the exact implementation you are trying to do, you could also use SharedPreferences to store these variables and change them as needed it when a new song is selected, etc. http://developer.android.com/guide/topics/data/data-storage.html#pref

Related

How to develop a Qlik Sense Extension that works in Mashups with multiple apps

My Extension doesn't work in Mashups that load objects from multiple apps.
It seems to work, when my extension is from the app that is loaded first by Qlik Sense. But if my Extension is in two different Apps, one of those works and the other doesn't.
TL;DR
Don’t use:
qlik.currApp()
Do use
qlik.currApp(this)
to make your Extension support being used in Mashups with multiple apps.
Detailed Explaination
If you need to call functions on the app behind the extension object, then you probably use qlik.currApp();
qlik.currApp() gives you the current App that is loaded. That is fine if only one app is loaded into an mashup. But if there are multiple apps, qlik.currApp() just gives you the first app, that it loaded.
According to the Qlik Sense Documentation on the currApp method:
qlik.currApp(reference)
Gets a reference to the current app. Use the currApp method in an
extension to get a reference to the app currently displayed.
Image you have two apps: A and B. A is loaded first. Then you include also an object from App B into the Mashup. But the type of object is an Extension and that extension uses qlik.currApp(), it is likely, that this object won’t work properly. That extension will call functions on app A even though it is from app B.
You can tell Qlik which app you want to reference. For that you need the reference to the extension instance. You get it as the this reference inside your paint method in the Extension Code:
paint: function(§element, layout){
var app = qlik.currApp(this);
// [...]
}
In case you have another closure inside the paint function, you need to save the this reference. If not, the this reference gets overwritten by the inner function object. A common case is to save the this reference to that:
paint: function(§element, layout){
var that = this;
loader.load(assets, function(){
var app = qlik.currApp(that);
// [...]
});
}

add a pass without instance of PKAddPassesViewController

In doc
To add a pass to the library:
Create an instance of the PKPass class for the pass, initializing it with the pass’s data.
Use the containsPass: method of the PKPassLibrary class to check whether the pass is in the library. Your app can use this method to detect the presence of a pass, even if it doesn’t have the entitlements to read passes in the library.
If the pass isn’t in the library, use an instance of the PKAddPassesViewController class to let the user add it.
Present the add passes view controller modally, with animation.
Is there any way to add a pass to my passbook without initializing PKAddPassesViewController?
Document clearly says, The PKPass class represents a single pass.
Also, there is no such method in PKClass to add it in the library.
So, you need to use PKAddPassesViewController that lets your app show a pass and prompt the user to add that pass to the pass library.

How to check if a word is defined in the English dictionary in cocoa-touch?

I am trying to make a crossword app for IOS but i Don't know that how to check if a string is valid english word or not.
How can i check it.
Is there any API or online facility to check it.
Thanks in Advance
Easy to do in iOS5 using the UIReferenceLibraryViewController class' +dictionaryHasDefinitionForTerm: method.
A UIReferenceLibraryViewController object provides a dictionary
service to look up the definition of terms. You create and initialize
a reference library view controller using the initWithTerm: method.
You pass the term to define as the parameter to this method and the
definition is displayed. You can present this view controller modally
or as part of another interface. On iPad, you can set the reference
library view controller as the content view controller of a
UIPopoverController object. Optionally, use the
dictionaryHasDefinitionForTerm: class method to check if a definition
is available for a given term before creating an instance—for example,
use this method if you want to change the user interface depending on
whether a definition is available.
There is no API for this.
In order to do this, you will need to have a dictionary file (text file or database) in your application bundle. One of the faster ways to check will be to load the dictionary into memory when the application launches so you don't have to read the file for each word. This may be overkill if you simply want hardcoded crosswords, but if you are randomly generating them then this is a must.

Why we use app delegate in our application

I am new in iphone development and i needed a array which i use to access in different class so my senior told me that you should declare in App delegate and access it another class in which it require, so i wrote line for that
MyAppAppDelegate * appObject =
(MyAppAppDelegate*)[[UIApplication sharedApplication]delegate];
I done my task successfully but i couldn't get it 100%. Someone tell that why we use this and in what exactly situation in which we've to use this?
AppDelegate is loaded first when you run your application as it contains window. So, the variable and objects you want to access throughout your project is declared in AppDelegate. You just have to create an instance and you can access all the objects in AppDelegate.
ApplicationDelegate can be useful as a singleton class but you have to use it with discretion - and there are varying opinions on this - but if you have a few global type properties or methods you want to recall from various other classes, and I emphasize few, then ApplicationDelegate may be a nice place to add these.
And yes, it is bad design - but you can get away with it if you are prudent and as #Sedate Alien mentions, take a look at dependency injection.
The purpose of ApplicationDelegate, by the way, is mainly to handle events like loading your application, when you return to home screen, when you come back from home screen, etc.

Trying to message a button from another class

I'm new to Objective-C so I may be doing this completely wrong, and if I am please correct me. I am trying to make a separate class in my iPhone app just for skinning buttons. My hope is that this will allow me to reuse as much code as possible but before I spend too much time on it, I would like to know if its possible/a good idea to send a message to a UI Control from another class, and if i can, how should I do it? right now im trying to pass the sender ID to my SkinTools class and message that but it doesn't look like it will allow me to message the layer object.
So, am I just completely off the wall here, or is this possible?
Consider looking into using the delegate pattern.
One could just use the addTarget:selector: method for this purpose. As target set the class you want to send the message to, as selector the method you want to call on the class.
You could add some iVars to your class, like id buttonTarget and SEL buttonSelector and create an initializer like -initButtonWithTarget:selector: to set these values on initialization.
It turns out categories was the answer I needed, then I can just add a skin method to each control I use. I can even put them all in the same file to make it easy to get to.