Skip to content
Snippets Groups Projects
Commit ce20d222 authored by NAEGEL BENOIT's avatar NAEGEL BENOIT
Browse files

Labs

parent f0ddae09
Branches
No related merge requests found
# Lab 2: Intents, Fragments
## Instructions
- Make regular and well-commented commits as needed
- Push at the end of the session
## New project creation
For all the following exercices:
- create a new project of the type "Empty Views Activity"
- choose the Kotlin language
- choose minimum API level 29
- keep the default build configuration language (Gradle Kotlin DSL)
## 1. Communication between activities
Write an application composed of two activities:
- in the main activity, the user enters his name, first name, phone number and email address. A "Clear" button allows to clear all fields. A "Send" button sends the information to a second Activity using an explicit intent.
All fields must be verified and filled in before sending the data.
- the second activity receives the data and displays it in a structured way on the screen.
All layouts must be created in XML, using a `ConstraintLayout`
1. Run and test your application
2. Internationalize the application, by adding at least a translation to another language
3. Save the entered fields in the first activity using `SharedPreferences`: the fields are restored when the application is restarted
4. Extend the previous application and add two buttons in the second activity:
- a "Share via email" button
- a "Share via SMS" button
The "Share via email" button should open an email app with pre-filled email address, using an implicit intent.
The "Share via SMS" button should open the SMS app with the phone number pre-filled and a default message, using an implicit intent.
## 2. Fragments
For more details on fragments, read the official documentation: https://developer.android.com/guide/components/fragments.html
A fragment is a reusable component that is attached to an activity. An activity can contain multiple fragments. A fragment can have a view (e.g. a user interface); to be visible, the fragment must be inserted into the layout of the current activity. The fragment has its own lifecycle, but it is constrained by the lifecycle of the host activity.
A fragment can be attached to an activity statically, in the activity's XML template (using a `FragmentContainerView`), or dynamically in the code. The latter option is the most interesting. To create and dynamically insert a fragment into an activity's view, you need to rely on the fragment manager `FragmentManager`. If a fragment has a view, its layout can be defined in an XML file.
## Fragments for navigation
Since 2018, Google recommends relying solely on fragments for displaying the different screens of the application. An Android application in this case consists of a single activity, the entry point of the application, and multiple fragments, each fragment representing a screen of the application. The activity will be used to host and display the different fragments. A portion of the activity's view will be dedicated to displaying the current fragment. Navigation between fragments can be done in different ways. In this lab, we will see how to navigate between fragments from the main activity.
## 1. Static fragment
- Create a new project with:
- a main activity `MainActivity`
- a fragment `BaseFragment` that will be statically inserted into the main activity's layout
- To declare a fragment, you need to create a class that inherits from `Fragment` and override the `onCreateView` method.
You could also declare a fragment with Android Studio by right-clicking on the package name, then `New` -> `Fragment` -> `Fragment (Blank)`, but in this case the class contains additional code that is not necessary for this exercise.
- The activity's layout , defined in the corresponding XML file, should contain:
- A text area initialized with the text "I am the main activity"
- A `FragmentContainerView` to display the fragment `BaseFragment`
- You can use either a `LinearLayout` or a `ConstraintLayout`
- The layout of the fragment, defined in the corresponding XML file `base_fragment.xml`, should contain:
- A text with the name of the fragment (e.g. "I am fragment base")
- A text editing area (EditText)
- An image (ImageView)
- A button (Button)
- You can use either a `LinearLayout` or a `ConstraintLayout`
- In the fragment class, the `onCreateView` method should look like this:
```kotlin
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
super.onCreateView(inflater, container, savedInstanceState)
return inflater.inflate(R.layout.base_fragment, container, false)
}
```
1. Test your application.
2. Override all the lifecycle methods of the fragment and display a message in the logcat (using `Log.d("Fragment", "onCreate()")` for example) and observe the lifecycle of the fragment.
3. Extends the application by adding a listener on the button of the fragment. When the button is clicked, the text of the editing area is displayed in the text area of the main activity.
## 2. Dynamic fragments
- Create a new project with:
- a main activity `MainActivity`
- two fragments `Fragment1` and `Fragment2` that will be dynamically inserted into the main activity's layout
- The activity's layout, defined in the corresponding XML file, contains:
- A text area for the title
- Two aligned buttons: button1, button2
- A view that shows the active fragment with a `FragmentContainerView`
- The layouts of the fragments, defined in the corresponding XML files, should contain:
- A text with the title of the fragment: "I am fragment1" - "I am fragment2"
- A text editing area
- An image
- A button
As previously, the layout of each fragment is inflated in the current layout in the method `onCreateView`.
In the activity, define two instances of `Fragment1` and `Fragment2` named `fragment1` and `fragment2` as class attributes.
- When button 1 is clicked, fragment1 is dynamically added to the main layout, in the `FragmentContainerView`. The fragment must also be added to the back stack.
- Similarly, when button 2 is clicked, fragment2 is displayed. The fragment must also be added to the back stack.
Dynamic fragment management is done through the fragment manager. You get the manager by calling the `getSupportFragmentManager()` method in the activity, then you perform a transaction from the manager with the `FragmentTransaction` class. You can add an animation using the `setTransition()` method. At the end of the transaction, you validate the transaction with a `commit`.
To add a new fragment to the main layout, you can write:
```kotlin
fragment = MyFragment() // MyFragment is a subclass of Fragment
val manager = supportFragmentManager
val transaction = manager.beginTransaction()
transaction.replace(R.id.myLayout, fragment)
transaction.commit()
```
1. Run and test your application
2. Observe the behavior of the back button when the fragments are displayed
3. We now want to send a message from `Fragment1` to `Fragment2` and vice versa: when the button in `Fragment1` is clicked, the text of the editing area is sent and displayed in `Fragment2` via the `MainActivity` and vice versa.
To do this, implement a callback from the fragment to the activity (see the course):
- create an interface in the fragment that defines a method to send the text
- the activity implements this interface and registers itself as a listener of the fragment
- when the button is clicked, the fragment calls the method of the interface of the listener with the text to send
- the listener (the activity) receives the text and sends it to the other fragment
4. The application must be robust to configuration changes (rotation of the screen, etc.). The fragments must be restored when the activity is recreated.
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment