Sharing Data Between Different Activities in Android Programming
Scope, Local, Global…What do they mean? Find out!
Have you ever heard of the word “scope”? The first thing that pops in your mind could be the zoom-lens that’s attached on top of a sniper rifle, but it means something completely different in the programming world. Scope is an important programming concept you need to understand to become a better programmer.
Today, we’ll talk about the concept of Scope and talk about why it is relevant to programming in Sketchware.
What you’ll need
- Sketchware — a mobile IDE on your smartphone
- Passion to learn! :-)
What you’ll learn
- Concept of Scope in programming
- Passing and retrieving data between activities
Scope refers to the visibility of variables. In other words, it means which parts of your program can see or use it. Let’s talk about it in terms of Sketchware. I will explain more as we progress through the example below.
Let’s say I created two views — view A and view B:
Note that each activity is linked to its own activity. You can notice that under each .xml files, a corresponding .java file follows after.
Under the activity of
view A, I created a variable named
a. However, when I move to the activity of
view B, I don’t see the variable anywhere! This is because any variable created under
view A has a scope that’s limited to only
view A. In other words, any activities other than
view A has no access to the variable. This concept is not only applied to variables, but also widgets and components. Check out the picture below for better understanding.
Also, take a look at another example below. This is an event generated by using MoreBlock. I named the function
scope, and it takes an argument called
variable. This variable is ONLY accessible inside this specific event. The concept of scope can be applied in so many ways — these examples were few of the many examples.
Local vs. Global
The variables only accessible by certain files or groups are called local variables. On the other hand, variables accessible anywhere are called global variables.
Sketchware keeps everything local by default.
Now that you understand the difference between local and global variables, a question may come to your mind.
Why do you guys keep everything locally? Wouldn’t it be easier for everyone to create a variable once and use it everywhere?
It’s a really good question. Sure, creating global variable is not a wrongdoing and it’s something developers often do to simplify some things up. However, creating global variables opens a whole another chapter in programming. These variables are usually called “static,” and they should be used only if you know what you’re doing. Creating global variables without having a deep understanding usually messes up the architecture. We wanted Sketchware to be not only a tool but also a great learning experience, so we took the traditional approach.
How can I transfer data to another Activity?
Since global variables are not a thing in Sketchware, there are two ways we can share data between Activities:
- Sharing data using File Component
- Passing value to another activity using Intent Component
File component lets you save data locally, so even if you exit the application the data remains up-to-date when you re-open the application. Even though the component is not shared globally when you create it, any persisted data is shared globally. Check out the previous Memo tutorial which uses File Component to share data.
Intent component lets you pass data to another Activity upon navigating. Unlike saving any data locally for future use, it’s just a temporary action. Let’s dig deeper by creating an example.
Start off by adding a new Intent component:
There are two blocks that handle the data transfer between activities:
Based on the shape of the blocks, note that all data is transferred as a String value. Transferring data happens in two steps:
putExtrablock to define any data you want to transfer in <key, value> form. Navigate to another Activity
- In Activity
xretrieve the value using
Let’s say we want to transfer some value from the
view B we created. We set the Screen to the screen we want to navigate to, and we put in the value we want to pass.
In View B, we retrieve the value by using the same key. Here, I set the value of the textview to the value I retrieved.
Perfect! This is what we wanted to achieve.
We talked about the concept of scope, and why Sketchware keeps everything locally. This, however, does not mean that you can’t share data between other activities. In summary, there are two ways you can transfer or pass data to another activity:
- Using File Component
- Using Intent Component
Hope you enjoyed this article! Happy coding! :-)
If you enjoyed this article, please press the 💚 below so others can find this article too!