Android Developer Fundamentalsをやる②

引き続きやっていきます。

Lesson 2: Activities

2.1: Create and Start Activities · Android Developer Fundamentals Course – Practicals

インテントとライフサイクル。

明示的インテント

https://gyazo.com/c849c34fa085f7d35adc8c926b5bc9f9

暗黙的インテント

https://gyazo.com/f172ecd03fd4ef91e9fa1cdd6037aa1f

Androidのライフサイクルはなかなか暗記できないので、図をプリントアウトして見えるところに貼っておく。

Homework Lesson 2

Homework Lesson 2 · Android Developer Fundamentals Course – Practicals

練習問題1

Lesson1でつくったHello Toastアプリを改造してSAY HELLOボタンをおすと新しいActivityに移動して現在のカウントを表示せよ。

https://gyazo.com/d24ba3ed30a560c1fea7c8ce49b34eae

MainActivity.java

   public class MainActivity extends AppCompatActivity {

    public static final String EXTRA_MESSAGE = "com.example.android.hellotoast.extra.MESSAGE";
            

    public int mCount = 0;
    private TextView mShowCount;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       mShowCount = (TextView) findViewById(R.id.show_count);
    }

    public void hello_btn_tapped(View view) {
        Intent intent = new Intent(this, Second.class);
        String score = mShowCount.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, score);
        startActivity(intent);
    }

    public void count_btn_tapped(View view) {
        mCount++;
        if (mShowCount != null) {
            mShowCount.setText(Integer.toString(mCount));
        }
    }
}

Second.java

   public class Second extends AppCompatActivity {

    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        mTextView = (TextView) findViewById(R.id.result_tv);

        Intent intent = getIntent();
        String score = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
        mTextView.setText(score);

    }
}

練習問題2

Lesson2でつくったImplicitIntentsアプリを改造して、カメラアプリケーションを起動せよ。
https://gyazo.com/b6e394e693f1c06a04710b59ef8bbdb5

MainActivity.java

   public class MainActivity extends AppCompatActivity {
   
   // 省略
   
   public void openCamera(View view) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivity(intent);
    }
  }