Android Development Course in HSE lecture #3

  • 353 views
Uploaded on

Activity Lifecycle, Views, Layouts

Activity Lifecycle, Views, Layouts

More in: Education
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
353
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
15
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Lecture #3
  • 2. Logs● Log.i(tag, msg); (information)● Log.v(tag, msg); (verbose)● Log.d(tag, msg); (debug)● Log.w(tag, msg); (warning)● Log.e(tag, msg); (error)
  • 3. DDMS
  • 4. Log.i("my_log_tag", "gotcha!");
  • 5. Activity
  • 6. public class MyActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_layout); }}
  • 7. Declaring "launcher" Activity<activity android:name=".MyActivity" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter></activity>
  • 8. Declaring new Activities<activity android:name=".AnotherActivity"></activity> НЕ ЗАБЫВАЙТЕ!
  • 9. Starting new ActivityIntent i = new Intent(context, NextActivity.class);startActivity(intent);context:● this● MyActivity.this● getApplicationContext()
  • 10. Activity Lifecycle
  • 11. Activity states● Resumed ("running")● Paused● Stopped
  • 12. Lifecycle methods● onCreate()● onRestart()● onStart()● onResume()● onPause()● onStop()● onDestroy()
  • 13. Starting an Activity● setContentView(R.layout.my_layout);● подготовка пользовательского интерфейса● инициализация переменных класса (если нужно)● вызывается единожды за жизненный цикл Activity● не выполняйте "тяжелых" задач в onCreate()
  • 14. Pausing and Resuming an Activity● Остановить анимации, звук, видео и т.д.● Сохранить состояние экрана (light auto- save only)● Очистить системные ресурсы (broadcast receivers, handlers to sensors (like GPS), camera, ...)● не выполняйте "тяжелых" задач в onPause()● В onResume(), наоборот, все восстанавливается (не забывайте, что вызывается каждый раз)
  • 15. Stopping and Restarting an Activity● Максимально очистить все ресурсы● Сохранить состояние экрана (heavy operations)● В onStart(), наоборот, все восстанавливается (не забывайте, что вызывается каждый раз)
  • 16. Recreating an Activity● onSaveInstanceState(Bundle savedInstanceState)● onRestoreInstanceState(Bundle savedInstanceState)● onCreate(Bundle savedInstanceState)
  • 17. ● onDestroy()● onRestart()
  • 18. github repo@Overrideprotected void onPause() { super.onPause(); Log.i("fsq", "onPause 1st");} НЕ ЗАБЫВАЙТЕ ВЫЗЫВАТЬ SUPER!
  • 19. Android UI
  • 20. LayoutParams android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_width="100dip" android: layout_height="100dip"
  • 21. К каждому View применимыследующие listenerы● OnClickListener● OnLongClickListener● OnTouchListener● OnDragListener● OnTouchListener● OnKeyListener
  • 22. padding and layout_margin● android:padding="5dip"● android:paddingRight="5dip"● android:layout_margin="5dip"● android:layout_marginRight="5dip" dp or dip
  • 23. TextView● android:text="@string/text"● android:textColor="@android:color/black"● android:textStyle="italic|bold"● android:textSize="14sp" sp
  • 24. Buttons● Button● ImageButton● android:text="@string/button_text"● android:src="@drawable/button_icon"● android:background="@android:color/transparent" (for ImageButton)● selector (for ImageButton)
  • 25. Button button = (Button) findViewById(R.id.button_send);button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Do something in response to button click }});
  • 26. ● button.setOnClickListener(this)● button.setOnClickListener(myClickListener)● android:onClick="clickMethod"
  • 27. switch (v.getId()) { case R.id.button1: // break; case R.id.button2: // break;}
  • 28. EditText● editText.getText();● android:inputType="textEmailAddress"● OnEditorActionListener● OnTextChangedListener
  • 29. ImageView android:src="@drawable/icon" android:scaleType="fitXY"
  • 30. ● CheckBox● RadioButton● ToggleButton
  • 31. Pickers
  • 32. ListView and GridView
  • 33. LinearLayout
  • 34. RelativeLayout ● android:layout_alignParentLeft=" true" ● android:layout_centerVertical="true" ● android:layout_toRightOf="@+id/id" ● android:layout_below="@+id/id"
  • 35. FrameLayout
  • 36. ScrollView
  • 37. gravity● android:gravity="center"● android:layout_gravity="center_horizontal|right"
  • 38. weight ● android:weight="1" ● android:layout_width="0dip"
  • 39. ToastToast.makeText(context, text, duration).show();
  • 40. Resources for multiple screens● drawable● drawable-xhdpi● drawable-hdpi● drawable-mdpi● layout● layout-land
  • 41. Other Resources● anim● assets● libs● values
  • 42. External jars Хранить в папке libs!
  • 43. Android Library Project
  • 44. MenuNo Context menu is a good Context menu (c)
  • 45. Homework● Activity Lifecycle● Views and Layouts