Rs. 1000 ($15) award when you find an issue that we struggle during class
1. Figma Design
2. Choosing a design pattern - > Declarative vs Imperative
3. Build
4. Test
Copy Paste Framework -> Learn Logic
// Mutable list (ArrayList)
val mutableList = mutableListOf(1, 2, 3)
mutableList[0] = 4 // Modifying an element
mutableList.add(5) // Adding an element
// Immutable list (List)
val immutableList = listOf(1, 2, 3)
// The following line would not compile since the list is immutable
// immutableList[0] = 4
val vs var
Source:
https://developer.android.com/guide/components/activities/activity-lifecycle
1. Reusability
2. Handling Configuration Changes: e.g. rotation
3. Responsive UI
(Most importantly Recommended by Android)
e.g. Don't re-invent the wheel all the time
Toast.maketext(context:this, "Hello World")
1. State Persistence -> Data is not lost but saved in Memory
2. Separation of concerns (Business Logic is Separate)
3. Testability
1. ViewModel shouldn't access View directly
2. One to many relationship, anyone can access ViewModel and observe it's data
3. Know how to pass context
Official Source: https://developer.android.com/guide/fragments/communicate
Sharing Data between Fragments
inside Fragment/ Activites -> Observer -> You (who subscribed)
.observe means subscribing (only who subscribe get to see)
by activityViewModels()
scope of activity
passing data between fragments
by ViewModels()
not passing data
rules to give to class
Feature | Classes | Interfaces |
---|---|---|
Method Implementation | Can have method implementations | Only method signatures, no implementations |
Inheritance | Single inheritance (extends one class) | Multiple inheritance (implements multiple interfaces) |
Usage | Defines objects, properties, and behavior | Defines contracts for behavior |
Abstraction | Abstraction of data and behavior | Higher level of abstraction, contract for behavior |
Multiple Inheritance | Not supported | Supported (through implementing multiple interfaces) |
Example | class MyClass { ... } | interface MyInterface { ... } |
val multiply: (Int, Int) -> Int = { a, b -> a * b }
fun main() {
val result = multiply(2, 3)
println("Result: $result") // Output: Result: 6
}
interface Operation {
fun perform(a: Int, b: Int): Int
}
class Multiply : Operation {
override fun perform(a: Int, b: Int) = a * b
}
fun calculate(op: Operation, a: Int, b: Int): Int {
return op.perform(a, b)
}
fun main() {
val multiplyOp = Multiply()
val result = calculate(multiplyOp, 2, 3)
println("Result: $result") // Output: Result: 6
}
typealias Callback = (String) -> Unit
fun performTask(callback: Callback) {
// Simulating some task
val result = "Task completed successfully"
callback(result)
}
fun main() {
performTask { result ->
println("Callback received: $result")
}
}
import java.util.function.Consumer;
interface Callback {
void onComplete(String result);
}
class Task {
void performTask(Callback callback) {
// Simulating some task
String result = "Task completed successfully";
callback.onComplete(result);
}
}
public class Main {
public static void main(String[] args) {
Task task = new Task();
task.performTask(result -> System.out.println("Callback received: " + result));
}
}
public class Main {
public static void main(String[] args) {
Adder adder = new Adder();
int result = adder.add(5, 7);
System.out.println("Result of adding 5 and 7 is: " + result);
}
}
class Adder {
public int add(int a, int b) {
return a + b;
}
}
fun main() {
val add: (Int, Int) -> Int = { a, b -> a + b }
val result = add(5, 7)
println("Result of adding 5 and 7 is: $result")
}