/ 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);
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.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)
            .setChooserTitle(title)
            .setType(mimeType)
            .setText(text)
            .getIntent();
    startActivity(intent);
}

Lifecycle

activity_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);
}
支付宝扫码打赏 微信打赏

若你觉得我的文章对你有帮助,欢迎点击上方按钮对我打赏

扫描二维码,分享此文章

Deserts

Deserts

CS专业在读;预备码农;科幻爱好者;年过半半百,一无所长。