Activity Android Club 2015
Agenda Hello Android application Application components Activity StartActivity
Hello Android! Let’s create your first app! Toast
Discover project structure src res AndroidManifest.xml gradle
Practice: create project Create Android project Name: MyProject
Practice: src Create another activity rather than MainActivity Name: AnotherActivity
Practice: AndroidManifest Make AndroidActivity main. Application should open AnotherActivity on start
Practice: gradle compile 'io.realm:realm-android:0.81.1'
Application components Activity Service BroadcastReceiver ContentProvider
Activity Activity – UI where user can interact with application One purpose
TaskBackstack
Game Button Back Button Which activity it will be?
Lifecycle
Example: lifecycle Logging lifecycle methods
Practice: lifecycle Find out how many methods will be called in this scenario: 1) Open app 2) Click home button 3)Open app from “Opened apps” 4)Click back button 5)Delete app from “Open apps”
How to start activity? startActivity startActivityForResult
startActivity Put button Set on click listener Open another activity
Practice: startActivity Put button on first activity: “Open third activity” Set on click listener startActivity using intent
Example: Pass data using Intent Pass data to another activity
Example: startActivityForResult Intent i = new Intent(this, SecondActivity.class); startActivityForResult(i, 1); Intent returnIntent = new Intent(); returnIntent.putExtra("result",result); setResult(RESULT_OK,returnIntent); finish();
Example: protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 1) { if(resultCode == RESULT_OK){ String result=data.getStringExtra("result"); if (resultCode == RESULT_CANCELED) { //Write your code if there's no result } } }
Practice: startActivityForResult Create one activity Create one button on first activity Open second activity when button clicked Second activity has 2 buttons: OKEY, BAD On result: show OKEY or BAD
Practice: startActivityForResult + data Create one activity Open second activity from first activity Put EditText to second activity Put button to return edittext’s text
Homework: Task 1 Create activity one Create 3 buttons on activity one: Red, Green, Blue Red: open RedActivity (background: red) Green: open GreenActivity (bg: green) Blue: open BlueActivity (bg: blue)
Homework: Task 2 Create ActivityOne Put edittext Users enters message and clicks “SEND” button SecondActivity shows message in TextView
Homework: Task 3 Create activity one Put one button Button open second activity EditText on SecondActivity User enters name in SecondActivity Show toast in FirstActivity: HELLO [NAME]!
Homework: Task 4 Create one activity. It should show counters onCreate():1 onStart():1 onResume():1 onPause():0 onStop():0 onPause():0
Questions? No question?
Thank you! Thank you very much for your attention!