|
![](/i/favi32.png) Pro Android with Kotlin@de android telegram Pro Android with Kotlin Developing Modern Mobile17
CHAPTER 3: Activities
You can specify
Intent.flags = Intent.
, where
is one from the list. In case
the activity attributes and caller flags contradict, the caller flags win.
Activities Returning Data
If you start an activity by using this:
startActivityForResult(intent:Intent, requestCode:Int)
it means you expect the called activity to give something back while it returns. The construct
you use in the called activity reads as follows:
val intent = Intent()
intent.putExtra(...)
intent.putExtra(...)
setResult(Activity.RESULT_OK, intent)
finish()
where inside the
.putExtra(...)
method calls you can add whatever data is to be returned
from the activity. You can, for example, add these lines to the
onBackPressed()
event handler
method.
For
setResult()
’s first argument, you can use any of the following:
Activity.RESULT_OK
if you want to tell the caller the called activity
successfully finished its job.
Activity.RESULT_CANCELED
if you want to tell the caller the called activity
did not successfully finish its job. You still can put extra information via
.putExtra(...)
to specify what went wrong.
Activity.RESULT_FIRST_USER + N
, with
N
being any number from 0, 1, 2,
..., for any custom result code you want to define. There is practically no
limit for
N
(the maximum value reads 2
31
− 1).
Note that you need to take care of also handling back-press events if you have a toolbar.
One possibility is to add to the
onCreate()
method lines as follows:
setSupportActionBar(toolbar)
supportActionBar!!.setDisplayHomeAsUpEnabled(true)
// The navigation button from the toolbar does not
// do the same as the BACK button, more precisely
// it does not call the onBackPressed() method.
// We add a listener to do it ourselves
toolbar.setNavigationOnClickListener { onBackPressed() }
|
|
|