Sunday, June 28, 2015

Android - Starting a New Activity - Explicit Intent

In Android, each screen or presentation is done via its own activity. So, we will have a main activity that runs when the application is first started. Launching a new activity is simple, using an explicit intent.

Let's say that we have a button that launches a new activity when it's clicked. Here's the code for doing this, using an explicit intent.

   public void openOtherActivityClick(View view) {
     // create an explicit intent and specify
     // the exact class name of the activity that we want
     Intent otherActivityIntent = new Intent(this, OtherActivity.class);
  
     // start the other activity
     startActivity(otherActivityIntent);
 }

With an explicit intent, we specify the exact class name of the activity that we want to start. Another way to start an activity, is with an implicit intent. Here's an example of how to use an implicit intent: http://travisdazell.blogspot.com/2015/01/android-implicit-intents.html

1 comment: