Activity lifecycle support. Some bugfix/refactoring.