Android @ Kiowok

Explicit Intents

Launching a New Activity

Assume that you are currently in the activity CurrentActivity and want to launch a new activity SecondActivity, you might enter the following code into an onClick event handler in CurrentActivity:

    Intenet intent = new Intent(this, SecondActivity.class);  
    startActivity(intent);

If onClick is implemented as as anonymous inner class, the first line would have to be replaced with Intenet intent = new Intent(CurrentActivity.this, SecondActivity.class);

Launching a Sub-Activity and Getting a Result

The current activity would need to implement both the code to start the subactivity:

    public static final int MY_REQUEST_CODE = 3;  // make up a number 
    Intenet intent = new Intent(this, SecondActivity.class);  
    startActivityForResult(intent, MY_REQUEST_CODE);

and also the code to process the result:

     @Override  
     protected void onActivityResult(int requestCode, int resultCode, Intent intent)  
     {  
		super.onActivityResult(requestCode, resultCode, intent);  
		 
		if(requestCode == MY_REQUEST_CODE && resultCode == Activity.RESULT_OK)  // other possible result: RESULT_CANCELED  
 		{  
			String value = intent.getStringExtra("my_info");   
			textView1.setText(value);  
		}  
     }  

The subactivity would need to create an intent for passing information back to the parent activity, and call finish to get the result sent back (possibly in an onClick event handler of its own):

    Intent back = new Intent();
    back.putExtra("my_info", "This is interesting");
    setResult(RESULT_OK, back);
    finish();
    

Note that the parent activity can send extra information to the subactivity in its intent. An activity can get its starting intent via a call to getIntent: Intent started = getIntent(); Also, to get the Bundle that holds all of the extra information, use:

    Bundle bundle = getIntent().getExtras();
    if (bundle != null) ...
    

Implicit Intents

This example sends out a couple of implicit intents -- one to handle a web URL, and another to load a phone number into a dialer.

If you setup a project:

  • name the project DemoInplicitIntent
  • name the main activity as DemoImplicitIntentActivity
  • setup a user interface with two Buttons and an EditText, with ids of webButton, dialButton, and textIn, respectively.

Code for the Activity class:

package com.example.demoimplicitintent;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class DemoImplicitIntentActivity extends Activity {
   EditText text;
   Button webButton, dialButton;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_demo_implicit_intent);

      text = (EditText) findViewById (R.id.textIn);
      webButton = (Button) findViewById (R.id.webButton);
      dialButton = (Button) findViewById (R.id.dialButton);

      webButton.setOnClickListener(new OnClickListener() {

         @Override
         public void onClick(View arg0) {
            String link = text.getText().toString().trim();
            Uri uri = Uri.parse(link);

            // ACTION_VIEW (for web access) does not need a manifest entry
            Intent viewIntent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(viewIntent);
         }
      });

      dialButton.setOnClickListener(new OnClickListener() {
         @Override
         public void onClick(View arg0) {
            // E.g., String number = "tel:248-522-3580";
            String number = "tel:" + text.getText().toString().trim();
            Uri callUri = Uri.parse(number);
            Intent intentCall = new Intent(Intent.ACTION_DIAL, callUri);
            startActivity(intentCall);
            // Does not need a "uses" clause in the manifest to just bring up the dialer
         }
      });
   }
}
Top