Thursday, December 21

Android : Music Machine (Intents & Broadcast Receivers)

Chap-1 For all intents and purposes:

Explicit and Implicit Intents:

Explicit Intents:
Intent intent = new Intent(this, DetailActivity.class);
startActivity(intent);
This intent where we name the class that's going to handle it, is known as explicit intent.

https://developer.android.com/guide/components/intents-filters.html
Implicit intents allow us to imply a solution to handling an intent without explicitly defining what is going to handle that intent.

When we start an activity this intent object is actually passed to the new activity. Because of this fact, we can add things to the intent and use it as a little delivery person to carry data somewhere else.
intent.putExtra("STRING_TITLE, "Gradle, Gradle, Gradle" );

let's see how to unpack this extra data.
Intent intent = getIntent();
String songTitle = intent.getStringExtra("STRING_TITLE");

Sometimes we start a second activity with the intention of getting some data to use in the original activity.
Now we also need to include a request code.
startActivityForResult(intent, REQUEST_FAVORITE);
Now we expecting a result to come back here to MainActivity. It requires that we implement another method in the activity to handle the result.
onActivityResult(int requestCode, int resultCode, Intent data)
{
}

In DetailActivity,
Intent resultIntent = new Intent();
resultIntent.putExtra("MainActivity.EXTRA_FAVORITE", isChecked);
setResult(RESULT_OK, resultIntent);
finish();

Roundtrip communication complete.



No comments:

Post a Comment