【Android基础 2.1】Activities and intents

I’ll use English in following blogs, because it’s easy to copy and paste. lol.
2018-11-13 15:31:21
codelab

An activity is started or activated with an intent. An Intent is an asynchronous message that you can use in your activity to request an action from another activity, or from some other app component. You use an intent to start one activity from another activity, and to pass data between activities.

An Intent can be explicit or implicit:

  • An explicit intent is one in which you know the target of that intent. That is, you already know the fully qualified class name of that specific activity.
  • An implicit intent is one in which you do not have the name of the target component, but you have a general action to perform.

In this blog, I’ll use explicit intent, implicit intent will be explained later.

What you will learn:

  • How to define parent and child activities for Up navigation.
  • How to start an Activity with an explicit Intent.
  • How to pass data between each Activity with an explicit Intent.

Create child activity

<activity android:name=".SecondActivity"
	android:parentActivityName=".MainActivity">
	<meta-data                   
               android:name="android.support.PARENT_ACTIVITY"             
               android:value="moe.leer.twoactivities.MainActivity"/>
</activity>

With android:parentActivityName attribute, We set the SecondActivity as the child of MainActivity. This relationship is used for Up navigation in your app: the app bar for the second activity will have a left-facing arrow so the user can navigate “upward” to the main activity.

child activity

The meta-data tag is same as android:parentActivityName.This tag is for android API levels 16 and below.Because the android:parentActivityName. is only available for API levels higher than 16. If your minSdkVersion in build.gradle is greater then 16, it’ ok to delete meta-data tag.

Send data to SecondActivity

In MainActivity:

    fun launchSecondActivity(view: View) {
        Log.d(TAG, "Button clicked!")
        val intent = Intent(this, SecondActivity::class.java).apply {
            putExtra(EXTRA_MESSAGE, editText.text.toString())
        }
        startActivity(intent)
    }

In SecondActivity:

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)

        val message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE)
        textMessage.text = message
    }

Return data back to MainActivity

We can use startActivityForResult to let second activity return back data. The onActivityResult method will be called when data is received.

In MainActivity:

    fun launchSecondActivity(view: View) {
        Log.d(TAG, "Button clicked!")
        val intent = Intent(this, SecondActivity::class.java).apply {
            putExtra(EXTRA_MESSAGE, editText.text.toString())
        }
        startActivityForResult(intent, TEXT_REQUEST)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == TEXT_REQUEST && resultCode == Activity.RESULT_OK) {
            text_header_reply.visibility = View.VISIBLE
            text_message_reply.text = data!!.getStringExtra(SecondActivity.EXTRA_REPLY)
            text_message_reply.visibility = View.VISIBLE
        } else {
            Toast.makeText(this, "NO data reply", Toast.LENGTH_SHORT).show()
        }
    }

In SecondActivity:

    fun returnReply(view: View) {
        val replyIntent = Intent().apply {
            putExtra(EXTRA_REPLY, editText2.text.toString())
        }
        setResult(Activity.RESULT_OK, replyIntent)
        finish()
    }

Summary

  • An Intent lets you request an action from another component in your app, for example, to start one Activity from another. An Intent can be explicit or implicit.
  • With an explicit Intent you indicate the specific target component to receive the data.
  • With an implicit Intent you specify the functionality you want but not the target component.
  • An Intent can include data on which to perform an action (as a URI) or additional information as Intent extras.
  • Intent extras are key/value pairs in a Bundle that are sent along with the Intent.

Homework

Question 1

What changes are made when you add a second Activity to your app by choosing File > New > Activity and an Activity template? Choose one:

  • The second Activity is added as a Java class, the XML layout file is created, and the AndroidManifest.xml file is changed to declare a second Activity.

Question 2

What happens if you remove the android:parentActivityName and the <meta-data>elements from the second Activity declaration in the AndroidManifest.xml file? Choose one:

  • The Up button in the app bar no longer appears in the second Activity to send the user back to the parent Activity.

Question 3

Which constructor method do you use to create a new explicit Intent? Choose one:

  • new Intent(Context context, Class<?> class)

Question 4

In the HelloToast app homework, how do you add the current value of the count to the Intent? Choose one:

  • As an Intent extra

Question 5

In the HelloToast app homework, how do you display the current count in the second “Hello” Activity? Choose one:

  • Get the Intent that the Activity was launched with.
  • Get the current count value out of the Intent.
  • Update the TextView for the count.
  • All of the above.
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值