Android screen orientation

disable orientation change

<activity android:name=".MainActivity"

Handling orientation state
Saving instance state

public void onSaveInstanceState(Bundle outState) {
    outState.putInt("ScoreKey", mScore);
    outState.putInt("IndexKey", mIndex);


Retrieving instance state

protected void onCreate(Bundle savedInstanceState) {

    if (savedInstanceState != null){
        mScore = savedInstanceState.getInt("ScoreKey");
        mIndex = savedInstanceState.getInt("IndexKey");

    } else {
        mScore = 0;
        mIndex = 0;

Android App Lifecycle

public class MainActivity extends AppCompatActivity {

protected void onCreate(Bundle savedInstanceState) {
Log.d("LifecycleTest", "onCreate");
protected void onStart() {
Log.d("LifecycleTest", "onStart");
protected void onResume() {
Log.d("LifecycleTest", "onResume");
protected void onPause() {
Log.d("LifecycleTest", "onPause");
protected void onSaveInstanceState(Bundle outState) {
Log.d("LifecycleTest", "onSaveInstanceState");
public void onRestoreInstanceState(Bundle savedInstanceState) {
Log.d("LifecycleTest", "onSaveInstanceState");

protected void onStop() {
Log.d("LifecycleTest", "onStop");
protected void onDestroy() {
Log.d("LifecycleTest", "onDestroy");

Android Enable USB-Debugging

  1. goto Settings > General > About phone > Software information and tap “Build number” seven times until it says “You are now a developer”
  2.  Goto Settings > General > Developer options and enable USB Debugging
  3. Drop down the notifications menu and click on “Tap for more USB options”. Most likely, the main title will say “Media Device (MTP)”. Choose the option “Send Images (PTP)”

JavaScript modules

    <script src="../js/constants.js" type="module"></script>
    <script src="../js/script.js" type="module"></script>


export const projects = [


import {projects} from "./constants.js";

MicroPython uos package

When you are familiar with python os and platform package You may wonder how to use it on ESP32 in MicroPython. Because this doesn’t work:

Traceback (most recent call last):
File "", line 1, in 
AttributeError: 'module' object has no attribute 'name'
>>> import platform
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: no module named 'platform'

UOS for the rescue

Continue reading “MicroPython uos package”

SQLite3: Python and SQL

Everything we did in last articles was a dry run because we just used SQLFiddle. So let’s start with a real database like SQLite.

SQLite is a file based DBRMS and can be used for e.g. web sites. The official docs say:

“SQLite works great as the database engine for most low to medium traffic websites (which is to say, most websites). [..] Generally speaking, any site that gets fewer than 100K hits/day should work fine with SQLite.”

Because Knight Industries is not Google, Amazon nor Facebook we can definitely use SQLite.

Creating and connecting to a database

In Python it is pretty easy to connect to a SQLite database:

from sqlite3 import connect
db_connection = connect('knight_industries.db')

If the file knight_industries.db does not exist, it will be created automagically. A nice little feature of the sqlite3 library.

But be careful: If You already have a database file and you mess up the path in the connect statement you will wonder why you cannot access your data, because a new file is created silently.

cursor = db_connection.cursor()
cursor.execute('''CREATE TABLE operatives (id INTEGER, name TEXT, birthday DATE)''')
cursor.execute('''INSERT INTO operatives (id, name, birthday) \
                  VALUES (1, "Michael Arthur Long", "1949-01-09")''')

cursor.execute('''SELECT * FROM operatives''')
print cursor.fetchone()