Android screen orientation

disable orientation change

<activity android:name=".MainActivity"
    android:screenOrientation="portrait"
    tools:ignore="LockedOrientationActivity">

Handling orientation state
Saving instance state

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

}

Retrieving instance state

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

    } else {
        mScore = 0;
        mIndex = 0;
    }

Android App Lifecycle

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("LifecycleTest", "onCreate");
}
@Override
protected void onStart() {
super.onStart();
Log.d("LifecycleTest", "onStart");
}
@Override
protected void onResume() {
super.onResume();
Log.d("LifecycleTest", "onResume");
}
@Override
protected void onPause() {
super.onPause();
Log.d("LifecycleTest", "onPause");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.d("LifecycleTest", "onSaveInstanceState");
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
Log.d("LifecycleTest", "onSaveInstanceState");
}

@Override
protected void onStop() {
super.onStop();
Log.d("LifecycleTest", "onStop");
}
@Override
protected void onDestroy() {
super.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

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

constants.js

export const projects = [
  "Project-A",
  "Project-B",
  "Project-C",
];

script.js

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:

>>> os.name
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")''')

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