How long does an Instance of my appWidget exist - android-appwidget

My simple Question:
How long does an instance of an appwidget exist?
To be more precise.
If I declare a static variable in an appwidget:
public static int myInt = 5;
Is it all the time accessable from my other activities which only launch if I click on the button?
Or does it just exist at an update which is defined in xml or if I click on the appwidget till the code has been finished.

Static variable exists as long as your process alive.

Related

Is there a way to configure Azure Table updates to preserve future/unknown properties/columns?

Suppose I create a model
public class Foo :TableEntity {
public int OriginalProperty {get;set;}
}
I then deploy a service that periodically updates the values of OriginalProperty with code similar to...
//use model-based query
var query = new TableQuery<Foo>().Where(…);
//get the (one) result
var row= (await table.ExecuteQueryAsync(query)).Single()
//modify and write it back
row.OriginalProperty = some_new_value;
await table.ExecuteAsync(TableOperation.InsertOrReplace(row));
At some later time I decide I want to add a new property to Foo for use by a different service.
public class Foo :TableEntity {
public int OriginalProperty {get;set;}
public int NewProperty {get;set;}
}
I make this change locally and start updating a few records from my local machine without updating the original deployed service.
The behaviour I am seeing is that changes I make to NewProperty from my local machine are lost as soon as the deployed service updates the record. Of course this makes sense in some ways. The service is unaware that NewProperty has been added and has no reason to preserve it. However my understanding was that the TableEntity implementation was dictionary-based so I was hoping that it would 'ignore' (i.e. preserve) newly introduced columns rather than delete them.
Is there a way to configure the query/insertion to get the behaviour I want? I'm aware of DynamicTableEntity but it's unclear whether using this as a base class would result in a change of behaviour for model properties.
Just to be clear, I'm not suggesting that continually fiddling with the model or having multiple client models for the same table is a good habit to get into, but it's definitely useful to be able to occasionally add a column without worrying about redeploying every service that might touch the affected table.
You can use InsertOrMerge instead of InsertOrReplace.

How To Call a String From A Different Method Than It Was Originally Declared

I'm trying to write an Oregon Trail type story in java. In one of the methods later on, you are asked to input your name. I've used this to get the name:
Scanner keys = new Scanner(System.in);
String name = keys.nextLine();
I would like to keep referring to the player as the name they entered in other methods and I'm unsure on how to call it. Any help is appreciated.
When you declare
String name = keys.nextLine();
You are creating a string inside the scope of that method. As you probably noticed, it's no longer accessible once the method finishes. Rather than storing the character name in a variable local to that method, you want to save it to a variable in an outside scope.
In Java's object oriented design, the ideal place to put that would be an instance variable for the relevant class. Say you have some master class called "Game". An instance of this class will represent a running game, have methods for interacting with the game, and hold data about the game. You could have an instance variable in Game declared as:
String playerName;
If that method is within Game, then you would simply have the code:
Scanner keys = new Scanner(System.in);
this.playerName = keys.nextLine();
Since you're assigning the name to a variable that exists outside the scope of the method, it will remain accessible to you later. The exact approach to this depends on how you structured your classes.
A more general solution, which could work better than the above solution depending on your code structure, would be to have that method return a String, rather than set one. For instance:
String getPlayerName() {
Scanner keys = new Scanner(System.in);
return keys.nextLine();
}
A method like that would return a string holding the name, which would allow you to work with it outside of the method.

Application Object Won't Share

I'm having issues with my Application Object. I am currently using a Service to simulate incoming data from an electronic game board. This data is represented as a 2D boolean array. Every five seconds the Service uses a method of the Application Object to update the array (setDetectionMap()). This array is being read by a Thread in my main Activity using another method (getDetectionMap()). After some debugging I am almost positive that the main Activity is not seeing the changes. Here is the code for my Application Object:
public class ChessApplication extends Application{
private static ChessApplication singleton;
private boolean[][] detectionMap;
public static ChessApplication getInstance(){
return singleton;
}
#Override
public void onCreate() {
super.onCreate();
singleton=this;
detectionMap=new boolean[8][8];
}
public boolean[][] getDetectionMap(){
return detectionMap;
}
public void setDetectionMap(boolean[][] newMap){
detectionMap=newMap;
Log.d("Chess Application","Board Changed");
}
}
I've checked my Manifest, I've rewritten my object declaration a dozen times, I've added LogCat tags to make sure that the code is executing when I think it should be, and I've even implemented the supposedly redundant Singleton code. Any ideas what could be causing this? Incidentally can anyone tell me how to view variable states as the activity is running? Thanks in advance.
Is your Activity calling getDetectionMap() to get the new map after the update occurs?
Because otherwise, it's holding onto a reference to the old boolean[][] array, wheras setDetectionMap(...) isn't actually updating the current data structure, it's just updating the "detectionMap" variable to point to a different one. As such, your main activity won't be aware of the swapout until the next time it calls getDetectionMap.
Easy fix: in setDetectionMap, manually copy values from newMap into detectionMap. Or, update the Activity's reference so it's looking at the right map.
One other observation entirely unrelated to the original question: It's quite unusual to override Application during Android development, and is usually considered a "code smell" unless you have a really good reason for doing so. In this case I imagine it's so that you can communicate between your service and Activity, but you create a middle-man where one isn't entirely necessary. Here's a useful SO thread on how to communicate directly between the two :)

Actionscript 3: Accessing an Instance via String?

This is not necessarily specific to ActionScript 3 but I could not think off the top of my head how you would access an instance via string or even if that is possible.
What I have going on is a function in one scene being passed a String which in my case is the name of a particular instance of a movie clip. Is there a way to use that String to access that instance on that scene, IE being passed the string I could change the alpha of that instance which matches the name of the string passed.
Thank you
basically, it is possible to access all public instances of an object/a scene by doing the following:
myMovieClip["myInstance"]
This is equivalent to
myMovieClip.myInstance
So, when you pass a string to a function in order to access a particular instance of that scene, you could, for example, do the following:
function myFunc(name:String, alpha:Number):void {
myMovieClip[name].alpha = alpha;
}
Hope that helps!

How to keep a local object not be GCed in c#?

I have this method:
private static void StartLicensePlateMonitor(Autofac.IContainer container)
{
var monitor = container.Resolve<LicensePlate.LicensePlateUploadMonitor>();
monitor.Start();
System.GC.KeepAlive(monitor); //does this work?
}
I want to keep monitor alive all time until program shut down, does the statement "System.GC.KeepAlive(monitor);" work?
No. The documentation for System.GC.KeepAlive() says that it ensures that the argument is alive up until the call to KeepAlive(). It's just a dummy function that the GC promises to not check for actual uses of the argument.
If you want an object to stay alive, ensure there is always a reference to it. It should be sufficient to store it in a private static field. If you have an arbitrary number of them, then store it in a List in a field.

Resources