2학년 모바일프로그래밍 9주차

서지현·2021년 12월 12일
0

모바일프로그래밍

목록 보기
3/4
  1. mainActivity -> subActivity -> mainActivity
  2. mainActivity -> subActivity (data)
  3. subActivity -> mainActivity (data)

1. mainActivity -> subActivity -> mainActivity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val intent = Intent(this, SubActivity::class.java)

        val openSubBtn = findViewById<Button>(R.id.openSubBtn)
        openSubBtn.setOnClickListener {
            Log.d("DKMobile","OPEN SUB button pressed")
            startActivity(intent)
        }
    }
}
  • To open another activity, declare intent & use “startActivity()”
class SubActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sub)

        val backBtn = findViewById<Button>(R.id.backBtn)
        backBtn.setOnClickListener {
            Log.d("DKMobile","BACK button pressed")
            finish()
        }
    }
}
  • To close(finish) activity, use “finish()”

2. mainActivity -> subActivity (data)

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        Log.d("DKMobile","oncreate()")

        val intentOpen = Intent(this, SubActivity::class.java)
        val openBtn = findViewById<Button>(R.id.openBtn)

        openBtn.setOnClickListener {
            Log.d("DKMobile","click to open sub activity")
            val num1w = findViewById<EditText>(R.id.num1)
            val num2w = findViewById<EditText>(R.id.num2)
            val num1 = num1w.text.toString().toInt()
            val num2 = num2w.text.toString().toInt()

            Log.d("DKMobile",num1.toString()+", "+num2.toString())
            intentOpen.putExtra("num1Extra",num1)
            intentOpen.putExtra("num2Extra",num2)

            startActivity(intentOpen)

        }
    }
}
  • To pass data to activity, use “intent.putExtra()”
class SubActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sub)

        val intentSub = intent

        val num1R = intentSub.getIntExtra("num1Extra",0)
        val num2R = intentSub.getIntExtra("num2Extra",0)

        Log.d("DKMobile","onCreate()")
        Log.d("DKMobile",num1R.toString()+", "+num2R.toString())

        val resultTxt = findViewById<TextView>(R.id.resultTxt)
        resultTxt.text = num1R.toString() +"+"+num2R.toString()+"="+((num1R+num2R).toString())

        val returnBtn = findViewById<Button>(R.id.returnBtn)
        returnBtn.setOnClickListener {
            Log.d("DKMobile","BACK button pressed.")
            finish()
        }

    }
}
  • To receive data from activity, use “intent.getXXXExtra()”

3. subActivity -> mainActivity (data)

class MainActivity : AppCompatActivity() {
    private val getResult =
        registerForActivityResult(
            ActivityResultContracts.StartActivityForResult()) { res->
            if(res. resultCode == Activity.RESULT_OK){
                Log.d("DKMobile","MainActivity onStart()")
                val value = res.data?.getIntExtra("result", 0)
                Log.d("DKMobile", "Received Result is "+value.toString())
                val mainResultView = findViewById<TextView>(R.id.mainResultView)
                mainResultView.text = "Result is "+value.toString()
                Log.d("DKMobile","MainActivity onResume()")
                Log.d("DKMobile","SubActivity onStop()")
                Log.d("DKMobile","SubActivity onDestory()")
                Log.d("DKMobile","MainActivity onPause()")
                Log.d("DKMobile","MainActivity onStop()")
                Log.d("DKMobile","MainActivity onDestory()")
            }
        }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        Log.d("DKMobile","MainActivity onCreate()")
        Log.d("DKMobile","MainActivity onStart()")
        Log.d("DKMobile","MainActivity onResume()")


        val intentOpen = Intent(this, SubActivity::class.java)
        val openBtn = findViewById<Button>(R.id.openBtn)

        openBtn.setOnClickListener {
            Log.d("DKMobile","OPEN SUB button pressed")
            val num1w = findViewById<EditText>(R.id.num1)
            val num2w = findViewById<EditText>(R.id.num2)
            val num1 = num1w.text.toString().toInt()
            val num2 = num2w.text.toString().toInt()

            Log.d("DKMobile","PutExtra"+num1.toString()+", "+num2.toString())
            intentOpen.putExtra("num1Extra",num1)
            intentOpen.putExtra("num2Extra",num2)

            Log.d("DKMobile","MainActivity onPause()")

            //startActivity(intentOpen)
            getResult.launch(intentOpen)
        }
    }
}
  • subActivity에서 값을 가져올때는 getResult.launch(intentOpen)문구를 작성해야 한다
  • startActivity(intentOpen) : mainActivity -> subActivity로의 메시지 전송
profile
안녕하세요

0개의 댓글

관련 채용 정보