Activity Overview & Intents
Activity Overview & Intents
What functionalities
can you identify?
Don’t worry about formalities,
just try to identify what
functions you think it has J
(Some) Contacts Functionality
Main Screen
Manage Accounts
<application
Notice…
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" • There is no main
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" function for Android
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
apps
<intent-filter>
<action android:name="android.intent.action.MAIN" /> • The Android system
<category android:name="android.intent.category.LAUNCHER" /> lunches a main
</intent-filter>
</activity>
activity as defined in
<activity android:name=".Choice01Activity" />
<activity android:name=".Choice02Activity" />
the Manifest
<activity android:name=".FinalActivity"></activity> Main -> Define an activity as an entry point to the app
</application> Launcher -> Indicates an activity as part of the
system’s app launcher
</manifest>
Creating an Activity
Activity
• When creating an activity by default…
By default, I mean by allowing Android Studio to do so…
This way, Android Studio automatically adds a layout file and an Activity details needed in the Manifest.
You can create an Activity from scratch, but make sure to create a layout file and define the activity in the
Manifest or else Android won’t recognize it.
Activity
• When creating an activity by default, notice that an
activity is a class that extends AppCompatActivity
• AppCompatActivity is a subclass of Activity
• Compat -> Compatible – implying this will work on older
devices
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Activity
• Additionally, by default, Activities are associated to a
Layout (and it resources) through setContentView()
• Can only have one ContentView at a time
• Typically, you’d have one activity associated to one layout
• Multiple activities could use the same layout [if reusable]
Just like what we did
public class MainActivity extends AppCompatActivity {
with a ViewHolder’s @Override If the ContentView
View in the protected void onCreate(Bundle savedInstanceState) { isn’t properly set up,
RecyclerView, super.onCreate(savedInstanceState); your Activity won’t be
setOnContactView() setContentView(R.layout.activity_main); able to access the
inflates the provided } the layout properly
layout
Activity à Context
• If you traces the superclass of Activity, you’ll find that
an Activity is also Context
• Context is an interface to global information about an
application | activity (i.e. it’s environment)
• It allows access to
resources and
classes
public class MainActivity extends AppCompatActivity { RECALL
private EditText first_name_et; When creating Views, we need to pass it a
private EditText last_name_et; Context object.
private Button add_btn;
private LinearLayout name_list_vll;
There are a few ways to reference a
@Override Context object:
protected void onCreate(Bundle savedInstanceState) { • Activity.this (the activity’s context)
super.onCreate(savedInstanceState); • View.getContext() [the View’s associated
setContentView(R.layout.activity_main); context, whatever that may be]
• getApplicationContext() [the context of
this.first_name_et = findViewById(R.id.firstname_etv); the entire application, not just the
this.last_name_et = findViewById(R.id.lastname_etv);
activity]
this.add_btn = findViewById(R.id.add_btn);
this.name_list_vll = findViewById(R.id.namelist_vll);
In this case, its actually better to use
this.add_btn.setOnClickListener(new View.OnClickListener() { MainActivity.this or view.getContext() –
@Override not getApplicationContext().
public void onClick(View view) {
TextView temp_tv = new TextView(MainActivity.this);
temp_tv.setText(last_name_et.getText() + ", " + first_name_et.getText()); Rule of thumb is that if the object that
name_list_vll.addView(temp_tv); required context is centered on the
} activity (like Views), use the Activity’s
}); context. Otherwise, use the
} Application’s context. J
}
Questions so far?
Multiple Activities
• Simple applications might
only need one activity, but
this is rarely the case
• Most apps have multiple
activities with each
corresponding to a specific
functionality
This implies that we’d need to to link
our Activities together somehow…
Moving from Activity to Activity
• In order to move to another Activity, we don’t instantiate
the second activity
• Instead, we send a “message” to the system and
request for the other activity to be launched
• For this, we’ll need to create + instantiate an Intent object
and request for that an activity be started, as such:
Intent intent = new Intent(SourceActivity.this, DestinationActivity.class);
startActivity(intent);
Context
Moving from Activity to Activity
• Once in the new activity, you can return to the previous
activity by clicking the back button
Design Tip:
Avoid creating a back
button if all it does is
redirect to the previous
screen. If its logic
mimics the system’s
back button, there’s no
need to include it.
Moving from Activity to Activity
• When using startActivity(), your app creates the
destination Activity and moves that to the front of the
Activity stack
In the visualization to the side, we see that the active
Activity (on top of the stack) is the View Contact screen.
This is where the user currently is.
Imagine being the user and pressing the back button – you’ll
basically be cycling through all the previously started
activities… and that would be pretty annoying.
Moving from Activity to Activity
• Don’t abuse startActivity()!
• As a solution: use finish() directly after starting an activity
• finish() signals to the OS that the current activity is to be
destroyed
Activity Stack
startActivity(i);
finish();
Let’s look at another example…
Activity Stack
startActivity(i);
public static String ADDEND_1_TAG = "ADDEND_1"; public static String ADDEND_1_TAG = "ADDEND_1";
public static String ADDEND_2_TAG = "ADDEND_2"; public static String ADDEND_2_TAG = "ADDEND_2";
… From: MainActivity …
To: AdditionActivity
Intent intent = new Intent( Intent intent = getIntent();
MainActivity.this, int addend1 = intent.getIntExtra(
AdditionActivity.class); MainActivity.ADDEND_1_TAG,
Intent.addExtra(ADDEND_1_TAG , 4) ADDEND_1; 4 0); // Second argument is for a default value
Intent.addExtra(ADDEND_2_TAG , 6) ADDEND_2; 6 int addend2 = intent.getIntExtra(
startActivity(intent); MainActivity.ADDEND_2_TAG,
0);
int result = addend1 + addend2
Expecting a Return Value(s)
• If we’re expecting the destination activity to return a
value to the source activity, we can make use of
ActivityResultLauncher
• Previously, the solution for this was to use
startActivityForResult(), but this has since been depreciated
Expecting a Return Value(s)
• To demonstrate
ActivityResultLauncher, let’s
consider the following application
with 2 activities
• First activity accepts 2 numbers and
sends them to the second activity for
adding
• Second activity adds both numbers
and sends the result back to the first
activity for it to be displayed
4
9
10
11 5
7
8
1
At this point, some of you might be
2 thinking this is a lot of effort to move
data around… and you’re right to think
3
so. However, given how Android treats
Activities, this is a way to transfer
data around
Questions?
Summary
• Activities are basic components of any Android
application
• Streamline functionalities of an application
• E.g. Home Page, Settings, View, Add/Edit, etc.
• Context allows access to resources and classes
• Is needed by certain objects, like Views
• Can be given at different levels: Activity, Application
Summary
• Its rare for an application to have only one Activity
• To move to another Activity, we need to create an intent
and use startActivity(i)
• We have to be mindful of how our Activities can stack and
should use finish() when needed
• We can also send data to other activities through
Extras
• If we’re expecting the destination to return a result, we
can use ActivityResultLauncher
For access to the project for the ActivityResultLauncher code, please see the Module in Canvas
Thanks everyone!