이 포스팅은 아래 구글 코드랩을 개인 학습용으로 정리한 글입니다.
Activity
An app usually consists of multiple screens(activities) that are loosely bound to each other
Main activity
Each time a new activity starts, the previous activity is stopped
When a new activity starts, that new activity is pushed onto the back stack and takes user focus (top of the back stack)
When the user is done with the current activity and presses the Back button,
An activity is started or activated with an intent
Intent:
You build the app in three stages
In the first stage:
In the second stage:
In the final stage:
Log.d(LOG_TAG, "Button clicked!");
private static final String LOG_TAG = MainActivity.class.getSimpleName();
➕In kotlin:
private val TAG : String = MainActivity::class.java.simpleName.toString()
package googleCodelabs.TwoActivities
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.core.content.PackageManagerCompat.LOG_TAG
private val TAG : String = MainActivity::class.java.simpleName.toString()
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun launchSecondActivity(view: View) {
Log.d(TAG, "Button clicked!")
}
}
Each new activity has its own layout and Java files, separate from those of the main activity.
They also have their own activity elements in the AndroidManifest.xml file.
In this task you add a second activity to our app, with its own layout.
You modify the AndroidManifest.xml file
-> to define the main activity as the parent of the second activity.
Then you modify the launchSecondActivity() method in MainActivity
-> to include an intent that launches the second activity when you click the button.
<activity
android:name=".SecondActivity"
android:exported="false">
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
<activity android:name=".SecondActivity"
android:label = "Second Activity"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=
"googleCodelabs.TwoActivities.MainActivity" />
</activity>
label attribute:
adds the title of the Activity to the app bar.
parentActivityName attribute:
you indicate that the main activity is the parent of the second activity.
This relationship is used for Up navigation in your app
-> the app bar for the second activity will have a left-facing arrow
-> the user can navigate "upward" to the main activity
meta-data element
you provide additional arbitrary information about the activity in the form of key-value pairs.
In this case, the metadata attributes do the same thing as the android:parentActivityName attribute
-> they define a relationship between two activities for upward navigation.
These metadata attributes are required for older versions of Android
-> the android:parentActivityName attribute is only available for API levels 16 and higher.
Intent intent = new Intent(this, SecondActivity.class);
➕ In kotlin:
val intent = Intent(this, SecondActivity::class.java)
startActivity(intent);
private val TAG : String = MainActivity::class.java.simpleName.toString()
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun launchSecondActivity(view: View) {
Log.d(TAG, "Button clicked!")
val intent = Intent(this, SecondActivity::class.java)
startActivity(intent)
}
}
Your intent object can pass data to the target activity in two ways:
-> in the data field or in the intent extras.
intent data: a URI indicating the specific data to be acted on.
intent extras: you can put additional information into extras
-> if the information you want to pass is not a URI
-> if you want to send more than one piece of information
intent extra: key/value pairs in a Bundle.
(Bundle: a collection of data, stored as key/value pairs.)
To pass information from one activity to another:
-> you put keys and values into the intent extra Bundle from the sending activity
-> then get them back out again in the receiving activity
In this task, you modify the explicit intent in MainActivity
-> to include additional data(in this case, a user-entered string) in the intent extra Bundle.
You then modify SecondActivity
-> to get that data back out of the intent extra Bundle
-> and display it on the screen.
private val TAG : String = MainActivity::class.java.simpleName.toString()
//public
val EXTRA_MESSAGE : String = "googleCodelabs.TwoActivities.extra.MESSAGE"
class MainActivity : AppCompatActivity() {
lateinit var mMessageEditText : EditText
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mMessageEditText = findViewById(R.id.editText_main)
}
fun launchSecondActivity(view: View) {
Log.d(TAG, "Button clicked!")
val intent = Intent(this, SecondActivity::class.java)
val message = mMessageEditText.text.toString()
intent.putExtra(EXTRA_MESSAGE, message)
startActivity(intent)
}
}
Intent intent = getIntent();
➕In Kotlin:
val intent: Intent = intent
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
➕In Kotlin:
val message : String? = intent.getStringExtra(googleCodelabs.TwoActivities.EXTRA_MESSAGE)
class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
//Get the Intent that activated this Activity
val intent: Intent = intent
val message : String? = intent.getStringExtra(EXTRA_MESSAGE)
val textView = findViewById<TextView>(R.id.text_message)
textView.text = message
}
setResult(RESULT_OK,replyIntent);
finish();
//public
val EXTRA_REPLY : String = "googleCodelabs.TwoActivities.extra.REPLY"
class SecondActivity : AppCompatActivity() {
lateinit var mReply : EditText
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
//Get the Intent that activated this Activity
val intent: Intent = intent
val message : String? = intent.getStringExtra(EXTRA_MESSAGE)
val textView = findViewById<TextView>(R.id.text_message)
textView.text = message
}
fun returnReply(view: View) {
mReply = findViewById(R.id.editText_second)
val reply : String? = mReply.text.toString()
val replyIntent = Intent()
replyIntent.putExtra(EXTRA_REPLY, reply)
setResult(RESULT_OK, replyIntent)
finish()
}
//public
val EXTRA_MESSAGE : String = "googleCodelabs.TwoActivities.extra.MESSAGE"
//public
val TEXT_REQUEST : Int = 1
class MainActivity : AppCompatActivity() {
...
fun launchSecondActivity(view: View) {
Log.d(TAG, "Button clicked!")
val intent = Intent(this, SecondActivity::class.java)
val message = mMessageEditText.text.toString()
intent.putExtra(EXTRA_MESSAGE, message)
startActivityForResult(intent, TEXT_REQUEST)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(requestCode == TEXT_REQUEST){
if(resultCode == RESULT_OK){
val reply = data?.getStringExtra(EXTRA_REPLY)
mReplyHeadTextView.visibility = View.VISIBLE
mReplyTextView.visibility = View.VISIBLE
mReplyTextView.text = reply
}
}
}
...
}
How to define log TAG constant:
- In Java:
private static final String TAG = MyClass.class.getSimpleName();
- In Kotlin:
private val TAG = MyClass::class.java.simpleName