Performance RemoteViews in Android - android

I am trying to write a small application in Android. My application is on Widgets. I have a basic doubt on using RemoteViews. I found that whenever , I update some button or some UI layout a new object of the remoteviews is created . Will that be a problem with the performance? an example code is this:
http://android-developers.blogspot.com/2009/04/introducing-home-screen-widgets-and.html
updateViews = new RemoteViews(context.getPackageName(), R.layout.widget_message);
even for updating the views everytime, new remoteview object is created. Please help me in understanding this.

No it isn't a problem. I'm working with StackViewsWidget example to build a honeycomb widget that has a RemoteViews for each card in the deck of views that shuffle buy, and each card is recycled and rebuilt from what I can tell in the debugger, and it's smooth as butter flipping through the views on my Xoom.

You might consider creating only one object using singleton pattern. This can be done by implementing a singleton for your RemoteViews class and then using this singleton to create an object of RemoteViews. Actually the problem lies with the memory of the machine on which you are running the android app. If you are able to scale that up by doing some profiling and performance settings then you can very well create new objects using the above code. But if you have a limitation then I would suggest the singleton pattern. This solution assumes that the machine you are working on has enough processing power.

Related

Using Android Fragments

When using Fragments in Android I can call fragment elements as following one.
HashMap<String, ArrayList<MyProduct>> orderAdap = InvocieProductFragment.mapOrderd;
In this case does it creates a new instance. Or else is it passing a copy or is it using the same resource. When I update a value on such an item does it updates in all places. I'm somewhat excited on this matter and can someone help me on this.
You're working on regular object instances. There's no magic involved. So you have to propagate changes yourself.

Glass Card.toRemoteViews() returns null

Using the Google Glass GDK, I'm trying to put together a simple app that displays / updates a live card. In my Service, I have the following method:
private void publishCard(Context context) {
Card updatedCard = new Card(context);
updatedCard.setText("Foo");
updatedCard.setInfo("Bar");
RemoteViews cardViews = updatedCard.toRemoteViews();
if (cardViews == null)
Log.e(TAG, "Appears to happen every time!")
// Then do some other stuff that fails because of a null RemoteViews
}
As you can see above, the null check seems to fail every time. Any idea why that might be?
My thought is that I'm calling this and passing in a Service as the context rather than an Activity, and maybe you're not supposed to do that? If that were the case, though, how would you be able to update a Live Card? I am capable of constructing a RemoteViews from an XML, but being able to use a Card would simplify things a lot.
That method is not currently implemented; you can follow issue 268 on our issue tracker if you want to keep track of the progress.
For now, you will have to create your own layout XML and use that to create the RemoteViews.

What exactly is context in Android and why is it needed?

I am new to Android development and Software development too.
I keep seeing this term called - 'context' in Android code.
I know that it's a class in android.content package, but I don't understand what exactly is it and why is it needed in so many places, especially in the constructors.
Can someone please explain this term to me.
As the name suggests, its the context of current state of the application/object. It lets newly created objects understand what has been going on. Typically you call it to get information regarding another part of your program (activity, package/application)
You can get the context by invoking getApplicationContext(), getContext(), getBaseContext() or this (when in the activity class).
Typical use of context:
Creating New objects: Creating new views, adapters, listeners:
TextView tv = new TextView(getContext()); ListAdapter adapter = new SimpleCursorAdapter(getApplicationContext(),..);
Accessing Standard Common Resources: Services like LAYOUT_INFLATER_SERVICE, SharedPreferences:
context.getSystemService(LAYOUT_INFLATER_SERVICE)
getApplicationContext().getSharedPreferences(name, mode);
Accessing Components Implicitly: Regarding content providers, broadcasts, intent
getApplicationContext().getContentResolver().query(uri,...);
Its copy from here

App widget getting content from database - update issues

I am making a app which allow users to create and delete spaces. Example of spaces: office, meeting room, shop, etc. A user will set a space when he is in that location. Example if he is at office he will set the space to office.
The spaces are stored in a database.
I have a home screen widget, and this widget needs to show the space that is set. Example needs to show office now.
I had some difficulty getting the widget to show these spaces. The problem is that the widget only updates after 30 min and when you do change it to update every second than you are faced with battery issues.
I only want the widget to update when a space is set.
Does anyone know how to deal with this problem or has some good advice.
Or know of any good tutorials which actually work this way.
I am new to android and java.
Yes, the link given by dave.c is largely correct, but if one is using a widget backed by a collection, a database in my case, a slight modification gives the intended result:
private void updateAllWidgets() {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getContext().getApplicationContext());
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(getContext().getApplicationContext(), ListWidgetProvider.class));
if (appWidgetIds.length > 0) {
// Tell the widgets that the list items should be invalidated and refreshed!
// Will call onDatasetChanged in ListWidgetService, doing a new requery
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.notes_list);
}
}
you can use this library: Android SQLiteAssetHelper
An Android helper class to manage database creation and version management using an application's raw asset files.
It helped me :)
If you want to update a widget programatically, you can use the approach in this answer

How to manage objects?

I am new to Android development.
I want to know how you all manage objects.
For eg we make object as A a = new A(); and then manage that object.
But, here, we call A.class;
My concern is that i dont want to call onCreate(),nor do i want to push UI screen.
I just want to make 1 static object for 1 screen;and want to manage it throughout my application
that is, instead of calling A.class; can i call A a = new A(),and manage that object without pushing,and whenever i need i push that screen. Is there someway ?
Thanks...
I just want to make 1 static object for 1 screen;and want to manage it throughout my application that is
That somehow describe what an Activity is for. Your complete question suggest that you have no idea how Android works and why it is meant to be. You should start at least with the fundamentals and than work through tutorials to get a feeling. Fundamentals can be found here: http://developer.android.com/guide/topics/fundamentals.html

Categories

Resources