/ Android

Android 入门笔记(二): Activity

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 an Activity, broadcastIntentto send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.

  • 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);

Same as startActivity(Intent, Bundle) with no options specified.

Pass Data


intent.putExtra(Intent.EXTRA_TEXT, textToSend);


Intent intent = getIntent();
if (intent.hasExtra(Intent.EXTRA_TEXT)) {
    String textEntered = intent.getStringExtra(Intent.EXTRA_TEXT);

Show "Back" Button

        android:value=".MainActivity" />


If you want to "back" to the previous activity, use below code in your onCreate method and onOptionsItemSelected:

public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == android.R.id.home) {
    return super.onOptionsItemSelected(item);

Implicit Intent

Common Intents on Google developer.

URI syntax:

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) {

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.IntentBuilderIntentBuilder is a helper for constructing ACTION_SEND and ACTION_SEND_MULTIPLE sharing intents and starting activities to share content.

ShareCompat.IntentReaderIntentReader 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)



Persist Data with onSaveInstanceState

protected void onCreate(Bundle savedInstanceState) {
	/*other things*/
    if (savedInstanceState != null) {
        String queryUrl = savedInstanceState.getString(SEARCH_QUERY_URL_EXTRA);
protected void onSaveInstanceState(Bundle outState) {
    String queryUrl = mUrlDisplayTextView.getText().toString();
    outState.putString(SEARCH_QUERY_URL_EXTRA, queryUrl);
Deserts Pan

Deserts Pan


Read More