How many times can you chain a single Intent object with putExtra()? - android

My question is essentially if the value that is in the intent object, when sent through a second time, does it have the same values again, and if so, is there a limit to how many times this can be done?

As we talk about Intent then it is used to Perform any Operation.
e.g intent.putExtra(key1,value);
All depends on the value. If your value is not null then you can use it as many times you want. It has no Limit.

You can use any number of times intent.putExtra(key,value);
but you have to make sure that the value and the key is not null and key is different from previous used keys.

Related

What the purpose of the frameTimeNanos in the Choreographer.FrameCallback.doFrame

In Choreographer.FrameCallback.doFrame event, their a value called frameTimeNanos. what exactly mean this value and more important how to use it, I mean in with scenario we need to take care of this value?

How to retrieve random children from Firebase

Suppose we would like to retrieve 15 random children from questions node having this database structured as below:
1. The first (intuitive and discussed) way of retrieving random children from Firebase is to retrieve the whole required parent node (questions as dataSnapshot) and then select some random children on the client-side. This method has been pointed out in many posts, like in this one here .
Obviously, this method has its downsides; for example when querying through a large sized parent node (e.g. over 10.000 children) retrieving such an amount every time would result in a huge bandwidth usage as well as a client side burden. (when we actually require only a small amount of children)
2. Moving on: another approach, as described here which uses an iterator somehow bypasses the whole client side burden, yet the huge bandwidth usage could still occur as we download the whole parent node every time.
3. An interesting approach is described in Tom's answer in this firebase discussion which proposes:
A hacky way of doing this would be to generate a random key and do a query with startAt().limit(1). I have a feeling this could hurt the performance of your firebase though, so this should not be an operation you perform often. We don't have a real random sample function.
This solution actually sounds pretty good, yet I am not sure how it would indeed impact my Firebase.
4. Another silly solution could actually be naming the question ids manually, so to speak, from 0 to N, therefore handling the random group of ids on the client side and retrieving the questions spot-on by knowing the actual name of nodes.
5. And lastly, I have come up with the following solution to which I ask if is more or less viable than the ones presented above: creating another parent containing the question ids only and when needed, one should retrieve this parent which is much "lighter" than questions parent . From there, I would have the specific random ids and I would only need to snipe for those children. To better understand my meaning, please check the below picture:
Now, from this method arises the following issue: is assigning (let's say) 15 eventListeners good practice? Could this actually slow up things? (Note: this applies to methods 3 and 4 as well)
And ultimately, which method is actually the optimal one when querying from a large database for some random children?
You can use the classic solution as I explained in this answer but if you are afraid of getting huge amount of data then use instead 15 listeners. There is nothing wrong in using listeners as long as you remove them according to the life-cycle of your activity. So, IMHO go ahead with 15 listeners.
We have 2 case here
case 1
If you want to grab all details of the random ids at once, then I suggest 1 listener to the parent node (get value of datasnapshot using pojo class).
case 2
If you want to get the details independently upon request then you will have to attach a listener to each (random id) that you want.
Concerning performance
Try to use only Listener For Single Value Events as they listen one time and then stop (better for performance).
Dont use Value Event Listener (because these listeners keep checking for changes and therefore bad performance as listeners increase).
EDIT
lets say you listened to (questions_ids) node, now you have access to the random id keys, store them in a String variable, and then inside the same listener add another listener to (questions) pointing to the id that you want to grab details
//first listen to question ids ref (the one with 15 ids)
question_ids_ref.addListenerForSingleValueEvent(...{
//grab the key of each (random id) and store in variable
String random_01=......;
//run another listener this time to questions ref
questions_ref.child(random_01).addListenerForSingleValueEvent(..{
//get details of random_01 and so on....
});
});

When to make an Object parcelable rather than just sending the primitive variables?

After researching for a while there is still something unclear for me:
When is it worth to implement Parcelable rather than just getting the variables and send them to a new Activity?
If i search for example for something like "RecyclerView click open new Activity", almost everyone posts code that extracts the values with getter methods in Activity 1 and sends them via multiple .putExtra calls to Activity 2. Almost no one seems to suggest to implement Parcelable.
I also fail to see where Parcelable saves effort or makes the code more maintainable, since i have to call .getXXX for every value in the target Activity anyways.
So let's say I have a RecyclerView, I want to open a new Activity on Button click and i want to send 3 variables out of 1 Object from 1 ArrayList. Should i send the variables directly over 3 .putExtra calls or implement Parcelable and send the whole Object?
In your case sending the 3 primitive values with putExtra should be enough. Parcelable is to send objects. Normally these objects contain objects which contain other objects or array of objects. In summary it is used to send complex objects. It's also necessary if you need to send an object to your service as part of the body of a request.
Don't use Parcelable if u have less no. Of data elements because serialization itself take some time. But sending them individually will be faster.
Using parcelable for something as trivial as this would be an overkill as it involves the overhead of constructing a new object for only a few values. It would be better if you just add the .putExtra() calls for the values you need to share, that should be enough, as the others before me said. :)

What is the best way to use threading on a sorting algorithm, that when completed, creates a new activity and gives its data to the new activity?

I will start this by saying that on iOS this algorithm takes, on average, <2 seconds to complete and given a simpler, more specific input that is the same between how I test it on iOS vs. Android it takes 0.09 seconds and 2.5 seconds respectively, and the Android version simply quits on me, no idea if that would be significantly longer. (The test data gives the sorting algorithm a relatively simple task)
More specifically, I have a HashMap (Using an NSMutableDictionary on iOS) that maps a unique key(Its a string of only integers called its course. For example: "12345") used to get specific sections under a course title. The hash map knows what course a specific section falls under because each section has a value "Course". Once they are retrieved these section objects are compared, to see if they can fit into a schedule together based on user input and their "timeBegin", "timeEnd", and "days" values.
For Example: If I asked for schedules with only the Course ABC1234(There are 50 different time slots or "sections" under that course title) and DEF5678(50 sections) it will iterate through the Hashmap to find every section that falls under those two courses. Then it will sort them into schedules of two classes each(one ABC1234 and one DEF5678) If no two courses have a conflict then a total of 2500(50*50) schedules are possible.
These "schedules" (Stored in ArrayLists since the number of user inputs varies from 1-8 and possible number of results varies from 1-100,000. The group of all schedules is a double ArrayList that looks like this ArrayList>. On iOS I use NSMutableArray) are then fed into the intent that is the next Activity. This Activity (Fragment techincally?) will be a pager that allows the user to scroll through the different combinations.
I copied the method of search and sort exactly as it is in iOS(This may not be the right thing to do since the languages and data structures may be fundamentally different) and it works correctly with small output but when it gets too large it can't handle it.
So is multithreading the answer? Should I use something other than a HashMap? Something other than ArrayLists? I only assume multithreading because the errors indicate that too much is being done on the main thread. I've also read that there is a limit to the size of data passed using Intents but I have no idea.
If I was unclear on anything feel free to ask for clarification. Also, I've been doing Android for ~2 weeks so I may completely off track but hopefully not, this is a fully functional and complete app in the iTunes Store already so I don't think I'm that far off. Thanks!
1) I think you should go with AsynTask of Android .The way it handle the View into `UI
threadandBackground threadfor operations (Like Sorting` ) is sufficient enough to help
you to get the Data Processed into Background thread And on Processing you can get the
Content on UI Thread.
Follow This ShorHand Example for This:
Example to Use Asyntask
2) Example(How to Proceed):
a) define your view into onPreExecute()
b) Do your Background Operation into doInBackground()
c) Get the Result into onPostExceute() and call the content for New Activty
Hope this could help...
I think it's better for you to use TreeMap instead of HashMap, which sorts data automatically everytime you mutate it. Therefore you won't have to sort your data before start another activity, you just pass it and that's all.
Also for using it you have to implement Comparable interface in your class which represents value of Map.
You can also read about TreeMap class there:
http://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html

Intent::putExtra() and EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

My code is as follows:
First, I was wondering about line 20:
I had two questions:
a. Why is MY_MESSAGE assigned to com.example.myfirstapp.MESSAGE?
b. What is com.example.myfirstapp.MESSAGE?
c. I mever made MESSAGE anywhere; is this automatically made like the variables in r.java file, or do i need to make it somewhere?
Secondly, about line 40: intent.putExtra(EXTRA_MESSAGE, message);
I am not sure if this method adds a message to the upcoming activity to be called or what... Partly, I am struggling to understand this due to not knowing the point of an Intent fully.
I want to read my 200 fundamental section on what everything is, but I have set deadlines and I have been told not to take that approach for the time being for this project
With given the explanation of the Android Docs , I know an intent is:
The Intent itself, an Intent object, is a passive data structure holding an abstract description of an operation to be performed
A.) Could someone explain what the intent is used for or give some better quick articles than just the docs?
B.) Explain what putExtra( ) does and and these parameters more clearly:
name The name of the extra data, with package prefix.
value The String array data value
An Intent is appropriately named; it's what you want to be done. As the documentation says:
Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.
By your code, you are familiar with starting an Activity via Intent:
new Intent(this, DisplayMessageActivity.class);
This uses your current Activity as the context from which to start the Intent, and gives the target class to launch. You already know this, I think. Basically, the Intent is just a guide for the Android device to follow so that it launches the right target with the right information.
Onto your real questions:
"What is the intent used for?" This is described above; basically, it's used to tell the OS what your target is, where it's coming from, and what data it should provide. You've seen most of this in action without realizing; this constructor is the one you've been using, detailing the "from" and "to" portions. When you use putExtra, you are providing the Intent with data it can give to the "to" part of the code.
The name parameter is best summed up by the documentation: "The name of the extra data, with package prefix." This is like a key in a HashMap; it is a string identifier of the content you are packaging. They tell you to use your package's prefix, just to prevent confusion. In your case, you should be using "com.SG.Three_Piece_Radio.YOURKEYNAME"; this does not have to be declared anywhere, nor is it a constant. Just a string. The value is just the contents of the extra (the data); this can be a ton of different things--short, int, String, Parcelable, and many more. (These can all be found in the various putExtras in the Intent docs.)
Once your Intent is received, you can use those same bits of data (for example, String myStr = getIntent().getStringExtra("com.SG.Three_Piece_Radio.YOURKEYNAME");) and do whatever you wish with them in the Activity you called.
I think people have been very helpful here in giving great explanations about Intent itself and its purpose. I got to learn a lot from these answers.
However, there was a small aspect that I think needs a little more explanation.
So to answer your very first question that says :-
a. Why is MY_MESSAGE assigned to com.example.myfirstapp.MESSAGE? b. What is com.example.myfirstapp.MESSAGE? c. I mever made MESSAGE anywhere; is this automatically made like the variables in r.java file, or do i need to make it somewhere?
My answer would be :-
So as all explained, putExtra is meant for carrying additional information/data along with the intent for the new activity that is going to be started. This additional information that putExtra carries is given in Intent in the form of a Key-Value pair.
In this Key-Value pair, the Key syntactically always has to be a String.
In your case, the value is also a String and the "key" can be any random string.
Now, to make sure that the system does not confuse your KEY with some other app's KEY you should always append the entire packet structure of the string along with it. And hence you use :-
com.example.myfirstapp.MESSAGE
where MESSAGE is actually the name of the key, (the String as is required, as I mentioned above) that would be associated with the string value that would be passed with the intent to the new activity.
Now you could have very well written the following as well :-
intent.putExtra("com.example.myfirstapp.MESSAGE", message);
instead of :-
intent.putExtra(EXTRA_MESSAGE, message);
but then this would reduce the flexibility of your code for changes to be made later. As for any changes in the key name you will have to change it everywhere. So to avoid this we rather assign the name of our key (in your case, MESSAGE) to a String variable (in your case EXTRA_MESSAGE).
This also makes it easier for other activities to reference this key by a simple String variable. And so to make it accessible to other activities (coupled with other self explained features)you make it as :-
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
Please correct me if I happened to miss something or went wrong somewhere.
The most common use of intents is to start new activities (screens)within an application (line 41). The extras Bundle is a way of passing data between activities. Extras are entered as key value pairs so EXTRA_MESSAGE is a key is used to identify a particular value so it can be retrieved and used by another activity.

Categories

Resources