Best practise question about passing information between Activity and Service - java

I have a question about best practise.
I have an Activity which is a form where the user fills in some details. There is a tick box which, if ticked will run a Service which will attempt to get a location.
If the activity is close (onDestroy) I write to a static boolean in a static class that this has happened.
When my Service gets a location it checks this static variable to see if it should pass the location back to the Activity (via a Handled Message). This bit works fine.
If the Activity has been saved to my database I need to give my service the row id so that it can update the database entry rather than pass the location back to the activity.
I decided, based on my limited Java and Android knowledge, to use another static field in my helper class which I can set with the rowID in my Activity. I can then access this rowid in my Service and go and get the database entry and update it. I am pretty sure that this will work but I am not sure it is appropriate
Any tips and advice would be appreciated, although 'that's not a good way to do it' with no other information is useless advice :)
Regards
Stephen

This guy has written an amazing tutorial: http://mindtherobot.com/blog/675/android-architecture-message-based-mvc/#more-675

Related

API to fetch the type of ActivityParameter which is applied to an activity in Enterprise Architect

I have activities on an activity diagram in Enterprise Architect for which there are some activity parameters set. The type of the Activity Parameter is set in the Parameter tab of the Properties as highlighted in the below picture. Is there any way to fetch the type of the activity parameter either through API or through EA Database.
Long answer short: it's EA.
Now for the gory details. You need to look into the ClassifierGUID which can only be obtained via t_object.classifier_guid. Now there you will either find a "real" guid of a t_object.ea_guid which has been used as a classifier or (Sparx strikes again!) some crude guid like
{EAINT000-B653-4f3c-A010-30205D67F5F5}
or
{EASTRING-B653-4f3c-A010-30205D67F5F5}
You can probably guess the rest :-/ The other elementary types you can create in the GUI and see the outcome in the database.
I obsevered that with 13.5. Maybe, maybe, or maybe not, Sparx has made use of EAElement.ClassifierName/Type to yield some meaningful result. I just got nothing on return here.

Android: Creating a new instance of a new activity

I'm confused regarding with some basic android development concepts, my question is not pointing at a particular code, thats why I dont include any.
Let's say that I have an activity inside of which I have a container in which I load a couple fragments (they are multiple instances of the same fragment), now the activity is populated, and inside one fragment I press a button that opens a new activity, it doesn't matter what may happen in that activity, the thing is that when I press a button it should take me back to the previos activity, I know that pressing the back button or using .finish(); will take me back to my already-populated activity, but I want to know, if that is the correct thing to do, or should I finish the activity as soon as i leave to the next one and when I want to go back create a new instance and repopulate it, if so, where should i store the variables?
Let's say that the fragments that I mentioned are "alarms" for an alarm application, and when I create it I call AlarmFragment newAlarm = new AlarmFragment(); and then I add that alarm to an arrayList in my alarms activity (java class) getListOfAlarms().add(getAlarmsAmount(), frag); which remains on the activity that has the fragment container, the question is, are these variables created in the right place? Because I am leaving them inside the activity itself right? What could happen if the activity is destroyed? I've been told that I should create an SQL database for storing those variables. I am not talking about long term saving but variables that I will be using at runtime
Can someone explain me these concepts a little bit? A link to a place where it is explained will be great too.
Your question seems like it has many parts.
In Part 1, this is what I think you are talking about:
1) how you decide to allow the user to get back to the first activity is really up to you. And 2) what you leave in the Back Stack is also up to you and what you want to define for the users' capabilities within your app. For example, if you want them to only be able to use a button that you define inside Activity 2 container, that is fine. However, you are not required to provide a button in Activity 2, and you are certainly allowed to use the Up Action in the App Bar for navigation. If I were you, I would read more about the Tasks and the Back Stack http://developer.android.com/guide/components/tasks-and-back-stack.html
You also mention this idea of having to "finish an Activity" with .finish(). I don't think that is usually necessary, but it is available to you if you want to use it based on what you decide for your app's logic (and what the user should and shouldn't be able to do).
With the Back button, Activity 1 will appear as if just initialized when you get back from Activity 2. Try it out. Also run some Log statements based on the simple diagram I provided and the Lifecycle "callbacks" (put these methods in your Activities and throw a Log statement in each to get a better sense of where you are in the Lifecycle) http://developer.android.com/guide/components/activities.html#Lifecycle
As for Part 2 of your question, I would try/set-up some of the above first, then start to experiment with a single variable to see what happens to it between Activities. There are a lot of "what ifs" to your question. You don't necessarily have to create a DB to store your variables, but that could certainly be an option. Take a look at the Developer Guide for most of the Data Storage options: http://developer.android.com/guide/topics/data/data-storage.html
If you are concerned about losing data when the Activity is destroyed, I might consider creating a database. Read the following for more info on recreating an Activity when you have gone elsewhere and are returning: http://developer.android.com/training/basics/activity-lifecycle/recreating.html In particular Saving Your Activity State: http://developer.android.com/training/basics/activity-lifecycle/recreating.html#SaveState
There's also an SO post on this: Saving Android Activity state using Save Instance State

Android - How to tell what Activities are currently open?

I'm falling into a bit of a dilemma. As I learn more and more about Android and Java, the more confused I am about the state of the building blocks of Android apps. (i.e. Activity, Service, Broadcast Recievers, Content Providers)
Since all of those building blocks are just Java classes, it's tough for me to wrap my head around them because there can be multiple instances of them. As of late the biggest challenge is with tracking which instance of the Activity is actually being created, started, resumed, etc;. I started playing around with Intent flags, and that just threw another wrench (or ten) into the equation.
Being able to tell their state would be indispensable and even better would be to see the current task stack/process id that the activity is associated with. At this point I'm just guessing whether or not FLAG_ACTIVITY_NEW_TASK, FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP are working properly.
Surely there is a solution. Is there any debugging tool that I can use to get an inside view of my app/process to see which components (specifically Activities) are in existence?
Some notes/thoughts I've had:
Should I create a unique static auto-increment int for each class to try and track it myself?
Is this what I want Get current visible activity to user?
Does this application manually keep track or is it really peaking inside of the process? https://play.google.com/store/apps/details?id=com.novoda.demos.activitylaunchmode
There has to be a debugging tool for this... -__-
Not sure if this will solve your understanding of the topic completely, but these guys did a nice app to try and visually see the main launch modes available for Activities > https://play.google.com/store/apps/details?id=com.novoda.demos.activitylaunchmode&hl=en_GB
Hope this helps in some way to explain things! :-)

What type of adapter should be used in this case? Singleton or shared preferences method?

I am developing android application. I did not plan my project that much before. So when I thought that I finished my task then I realized that I am not done at all. I have some issues that I figured it out now. I want to go again from the beginning of the project.
I want to get the list of training that is in external database and data will be populated manually using SQLite browser (only training table, columns are _id, title, description, date and time, location.). And onclick of that item in list, it should start new activity with holding the training session all along the application activity. [On Training, people will fill their personal information (Now, person table), where the data of person should be stored in the particular training session].
Now, my questions are;
1. What type of Adapter should I use in this case and why?
2. Should I use shared preferences or Singleton method to keep the session of training all activities of application?
Both of those question are determined by your requirements for this software. You said it yourself that you 'did not plan my project that much before'. Sounds like you're at a point where you need to do some hard thinking about your requirements. Requirements will, in turn, determine the technologies/ methodologies you need to use.
With the information you've provided, it would be impossible for anyone to provide advice that would be anything better than a guess.

Best practices to get data and save it

I have an android application and in one of my activities I am making a call to get say "Customers", this call is made to an external API, when I get the response I get it as a JSON object. The problem i am having is that I have a ListView in the activity and when you click on of its items it shows you the details but then when you hit the back button I have to make the call again to populate it. In Samsung Galaxy 4S it seems to keep the data of the list view but in the HTC android incredible it's blank. So what I did is, make it rebind OnResume(), this fixed the issues for both BUT the consequence is making another call to that server. When its 10 or 100 customers it doesnt matter but I know that there are some accounts that have up to 5000 and I am sure it will crash.
What are my options to improve performance on this issue with Android?, I tried a static variable but at some point that object got cleared too.
How do Android applications usually handle this cases where the data is retrieved from API's and they need to be stored through out the application and there is no need to make another call for the same information?, I was thinking on static object but i want to make sure I do this the right way.
You have a couple of options.
1) You can cache the data in memory. For example you can make a static cache or cache the data within the Activity or the App object. If you are doing this in only one view and if it is not a lot of data, this might be an ok solution. However, if you have to do this for many activities and there is a lot of data that has to be cached, you might want to go for option 2. Also storing data in memory in android, does not mean it won't be garbage collected (in some cases, even if you have a reference to it.)
2) You can cache the data in the internal storage and refresh it from time to time.
You can find more info about the internal storage and how to use it here: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
Basically, you store the response within the internal storage under a specific identifier. The next time you open the activity, you check if the storage has data for that identifier and if yes, you read it and display it. If no, you make the API call.
Keep in mind, that you will have to refresh the cache from time to time.
I had the problem with ListViews on my application too. What I did is that I wrote a custom adapter and that solved the issue..
However the thing you can do is to make a global variable and save the returned results to it. When your application wants to call the server, check the variable, if it's null make the call, if it's not then just draw the ListView with the already fetched data..
Keep in my mind, to implement a refresh button, you need to skip the check.

Categories