Home » Android » core » activity » Launching new activities with intents

About Byron Kiourtzoglou

Byron Kiourtzoglou
Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

Launching new activities with intents

In Android you will frequently come up will a need to launch a new Activity from inside your own Application. This is important when you want your Application to have multiple Activities. It is also very useful when you want to use an existing Activity in your system. New Activities are usually launched using Intents.
 
 
 
 
 
 
 

So, in order to launch new Activities you have to:

  • Create a new Intent with the appropriate source
  • Use startActivity to launch the new Activity
package com.javacodegeeks.android.apps.moviesearchapp;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.javacodegeeks.android.apps.moviesearchapp.model.Movie;

public class MoviesListActivity extends ListActivity {

    private static final String IMDB_BASE_URL = "http://m.imdb.com/title/";

    private ArrayList>Movie> moviesList;
    private ArrayAdapter<Movie> moviesAdapter;

    @SuppressWarnings("unchecked")
    @Override
    public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.movies_layout);

  moviesList = (ArrayList<Movie>) getIntent().getSerializableExtra("movies");

  moviesAdapter = new ArrayAdapter<Movie>(this, android.R.layout.simple_list_item_1, moviesList);

  setListAdapter(moviesAdapter);

    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {

  super.onListItemClick(l, v, position, id);

  Movie movie = moviesAdapter.getItem(position);

  String imdbId = movie.imdbId;

  if (imdbId==null || imdbId.length()==0) {

longToast(getString(R.string.no_imdb_id_found));

return;

  }

  String imdbUrl = IMDB_BASE_URL + movie.imdbId;

  Intent imdbIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(imdbUrl));

  startActivity(imdbIntent);

    }

    public void longToast(CharSequence message) {

  Toast.makeText(this, message, Toast.LENGTH_LONG).show();
    }

}

 
This was an example on how to Launch new Activities with Intents in Android.

Related Article:

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments