0% found this document useful (0 votes)
42 views5 pages

Exercise 6 - Activity Lifecycle

The document discusses the lifecycle of an Android activity. It describes how to create a new project and activity, override lifecycle methods like onCreate, onStart, onResume etc. and implement logging in these methods to see the activity state changes when actions are performed like launching the app initially, rotating the screen etc.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views5 pages

Exercise 6 - Activity Lifecycle

The document discusses the lifecycle of an Android activity. It describes how to create a new project and activity, override lifecycle methods like onCreate, onStart, onResume etc. and implement logging in these methods to see the activity state changes when actions are performed like launching the app initially, rotating the screen etc.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Activity lifecycle

1. Project:
- File > New > New project > Empty Views Activity -> Click Next.
- Choose Project name, location, language (java), min SDK Groovy DSL -> Click
Finish
2. Override methods:
- Open [Link]
- Right click > Generate > Override methods > onStart(), onResume(),
onPause(), onStop(), onDestroy()

1
3. Implement methods:
import [Link];
import [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
[Link](this);
setContentView([Link].activity_main);

[Link](findViewById([Link]
), (v, insets) -> {
Insets systemBars =
[Link]([Link]());
[Link]([Link], [Link],
[Link], [Link]);
return insets;
});

2
Log.i("MainActivity", "State: Created");
}

@Override
protected void onStart() {
[Link]();
Log.i("MainActivity", "State: Started");
}

@Override
protected void onResume() {
[Link]();
Log.i("MainActivity", "State: Resumed");
}

@Override
protected void onPause() {
[Link]();
Log.i("MainActivity", "State: Paused");
}

@Override
protected void onStop() {
[Link]();
Log.i("MainActivity", "State: Stopped");
}

@Override
protected void onDestroy() {
[Link]();
Log.i("MainActivity", "State: Destroyed");
}
}

Notes:
- Log.e: This is for when bad stuff happens. Use this tag in places like inside a catch
statement. You know that an error has occurred and therefore you're logging an
error.
- Log.w: Use this when you suspect something shady is going on. You may not be
completely in full on error mode, but maybe you recovered from some unexpected
behavior. Basically, use this to log stuff you didn't expect to happen but isn't
necessarily an error. Kind of like a "hey, this happened, and it's weird, we should
look into it."

3
- Log.i: Use this to post useful information to the log. For example: that you have
successfully connected to a server. Basically use it to report successes.
- Log.d: Use this for debugging purposes. If you want to print out a bunch of messages
so you can log the exact flow of your program, use this. If you want to keep a log
of variable values, use this.
- Log.v: Use this when you want to go absolutely nuts with your logging. If for some
reason you've decided to log every little thing in a particular part of your app, use
the Log.v tag.
- [Link]: Use this when stuff goes absolutely, horribly, holy-crap wrong.

4. Run app
The Logcat window displays activity states:

5. Rotate screen

Auto
Rotate

4
Rotate
screen

The Logcat window displays activity states:

You might also like