Skip to content

Instantly share code, notes, and snippets.

View Mercandj's full-sized avatar
😀
Working on Android Apps

Jonathan Mercandalli Mercandj

😀
Working on Android Apps
View GitHub Profile

Swift 4 - List of listeners

Note: I'm an Android developer that trying to create iOS apps. So I may be missing some notions. Sorry for that.

Problem

In many managers and buisness classes, we could want to have a listeners mechanism (sometimes called events) with some notions to register (or addListener), unregister (or removeListener) and notify (or broadcast) all the listeners registered.

So the problem is the following: "How to create an implementation of the following contract bellow".

@Mercandj
Mercandj / android-view-pager.md
Last active April 19, 2019 09:02
Android ViewPager: listen displayed step

ViewPager

Track ViewPager position


drawing drawing

Programmation: Yet another MVP implementation

Description

When a program is dealing with views and business logic, some principles could become usefull:

    1. Testable code
    1. Code that abstract the platform (to be reuse, to be unit testable)
    1. Split notions of business logic from the user interface

Keep in mind that this MVP is designed to be use in every front-end developement dealing with "view": Android, iOS, Unity, Web etc.

#!/usr/bin/env bash
ANDROID_SERIAL_NUMBER=$1
if [[ "$ANDROID_SERIAL_NUMBER" = "" ]] ; then
printf "Missing android serial number after the command.
To get it -> adb devices: \n"
adb devices
exit 0
fi
@Mercandj
Mercandj / open-url.sh
Last active September 20, 2019 08:35
#!/usr/bin/env bash
DEVICE=$1
URL=$2
if [[ "$DEVICE" = "" ]] ; then
printf "Missing device arg\n"
printf "First arg should be the device\n"
printf "Second arg should be the url\n"
adb devices
#!/bin/bash
# Parameters: nothing
# Required `$ANDROID_NDK` in your path. For example /Users/jonathan/Library/Android/sdk/ndk-bundle
# Script to generate pre-build opus files
BASE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
if [ -z "$ANDROID_NDK" ]; then
echo "Please set ANDROID_NDK to the Android NDK folder"
@Mercandj
Mercandj / android_asynchronous.md
Last active February 2, 2022 23:52
android_asynchronous

Android: Asynchronous

Note that by default, on Android, everything is on the main thread (also called, the UI thread). In order to do heavy task or network calls, you must be on another thread to avoid to block the UI thread.

Once your work done on non-main-thread, you will need to come back on the main thread in order to update the UI.

Keep in mind that every Activity, View or Fragment will crash if UI is modified on a non-main-thread.

  • This document is to highlight some way to work with asynchronous on Android.

Android: Application initialization

On Android, there is a common issue that developers must solve: Deal with heavy load at application start.

1. Application.onCreate()

The easier solution is to put the initialization on the Application

class MainApplication: Application() {

Immutable lists

Introduction

Immutable lists can be misleading. Here I will use Kotlin. This snippet is viable on Java, C#, Swift... too.

I'm assuming you understand why immutable objects are "more robust" to manipulate data.

Here a "book" with "pages".

Android: Activity restoration

When you user click Home on any of your activity, the system will save the state of the activity stack and the state of your top-activity.

Then, when your user go back to your application, this activity and the activities stack will be restored on a new JVM.

How to test it?

  • Open your app
  • Go to the activity you want to test