Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Activity LifeCycle入門

Activity LifeCycle入門

Benoît Quenaudon

November 01, 2016
Tweet

More Decks by Benoît Quenaudon

Other Decks in Programming

Transcript

  1. Activity とは? TickerActivity.java public class TickerActivity extends AppCompatActivity { private

    Counter counter;   @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); counter = new Counter(); /* ... */ }   public void buttonClicked(View view) { counter.incrementeByOne(); } } activity_ticker.xml <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout>   <TextView />   <TextView />   <Button /> </android.support.constraint.ConstraintLayout>
  2. Activity State ステート メモリにある ? ユーザが見える? フォアグラウンド 不存在 ✕ ✕

    ✕ ストップ ◎ ✕ ✕ ポーズ ◎ ◎ 一部デモ ✕ ランニング (レジューム) ◎ ◎ ◎
  3. onSaveInstanceState public class TickerActivity extends AppCompatActivity { private static final

    String COUNTER_KEY = "COUNTER_KEY"; // 略 @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt(COUNTER_KEY, counter.getCount()); } }
  4. Using State Bundle public class TickerActivity extends AppCompatActivity { @Override

    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (savedInstanceState == null) { counter = new Counter(); } else { counter = new Counter( savedInstanceState.getInt(COUNTER_KEY, 0) ); } } }
  5. Reference • Activities in the Wild ◦ https://goo.gl/EL0uW9 • Deep

    Dive Into State Restoration ◦ https://goo.gl/kAIpBD • onSaveInstanceState ◦ https://goo.gl/q0nF8X • Managing the Activity Lifecycle ◦ https://goo.gl/Gt7pIy