Activity and Intent
An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View)
.
While activities are often presented to the user as full-screen windows, they can also be used in other ways: as floating windows (via a theme with windowIsFloating
set) or embedded inside of another activity (using ActivityGroup
). There are two methods almost all subclasses of Activity will implement:
-
An intent is an abstract description of an operation to be performed. It can be used with
startActivity
to launch anActivity
,broadcastIntent
to send it to any interestedBroadcastReceiver
components, andstartService(Intent)
orbindService(Intent, ServiceConnection, int)
to communicate with a backgroundService
. -
An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.
Start Activity
Class destinationClass = ChildActivity.class;
Intent intent = new Intent(context, destinationActivityClass);
startActivity(intent);
Same as startActivity(Intent, Bundle)
with no options specified.
Pass Data
putExtra
intent.putExtra(Intent.EXTRA_TEXT, textToSend);
getIntent
Intent intent = getIntent();
if (intent.hasExtra(Intent.EXTRA_TEXT)) {
String textEntered = intent.getStringExtra(Intent.EXTRA_TEXT);
}
Show "Back" Button
<activity
android:name=".DetailActivity"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
Note:
If you want to "back" to the previous activity, use below code in your onCreate method and onOptionsItemSelected:
this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
onBackPressed();
}
return super.onOptionsItemSelected(item);
}
Implicit Intent
Common Intents on Google developer.
URI syntax:
scheme:[//[user[:password]@]host[:port]][/path][?query][#fragment]
MIME(Multipurpose Internet Mail Extensions):
Content-Type: [type]/[subtype]; parameter
Call Browser to show a web page:
private void openWebPage(String url) {
Uri webpage = Uri.parse(url);
// Here, we create the Intent with the action of ACTION_VIEW.
// This action allows the user to view particular content.
Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
// This is a check we perform with every implicit Intent that we launch.
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
Maps URI
String address = "Yanshan University";
Uri.Builder builder = new Uri.Builder();
Uri uri = builder.scheme("geo").path("0,0")
.appendQueryParameter("q", address)
.appendQueryParameter("z", "10").build();
Share Intent
ShareCompat.IntentBuilder
IntentBuilder is a helper for constructing ACTION_SEND
and ACTION_SEND_MULTIPLE
sharing intents and starting activities to share content.
ShareCompat.IntentReader
IntentReader is a helper for reading the data contained within a sharing (ACTION_SEND) Intent.
private void shareText (String text) {
String mimeType = "text/plain";
String title = "Learn how to share";
Intent intent = ShareCompat.IntentBuilder.from(this)
.setChooserTitle(title)
.setType(mimeType)
.setText(text)
.getIntent();
startActivity(intent);
}
Lifecycle
Persist Data with onSaveInstanceState
@Override
protected void onCreate(Bundle savedInstanceState) {
/*other things*/
if (savedInstanceState != null) {
String queryUrl = savedInstanceState.getString(SEARCH_QUERY_URL_EXTRA);
mUrlDisplayTextView.setText(queryUrl);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
String queryUrl = mUrlDisplayTextView.getText().toString();
outState.putString(SEARCH_QUERY_URL_EXTRA, queryUrl);
}