---
title: "Start a new activity within an Activity in Android"  
description: "Previously , we learn how to implement contextual menus on Long press in android : Contextual Menu Implementation in Android. Now, here we unfold some"  
author: "Anonymous User"  
published: 2015-02-04  
updated: 2020-03-04  
canonical: https://www.mindstick.com/articles/1569/start-a-new-activity-within-an-activity-in-android  
category: "android"  
tags: ["android", "android activity", "android intent"]  
reading_time: 8 minutes  

---

# Start a new activity within an Activity in Android

Previously , we learn how to implement contextual menus on [Long press](https://www.mindstick.com/forum/23140/how-to-handle-capture-a-long-press-in-windows-store-apps) in android : **Contextual Menu [Implementation](https://answers.mindstick.com/qa/44913/who-is-the-minister-of-statistics-and-programme-implementation) in Android**. Now, here we unfold some intent concepts!!\

You can start another activity within the same application by calling startActivity(), passing it an Intent that describes the activity “[class name](https://www.mindstick.com/forum/12871/how-to-get-class-name)” you want to start. Alternatively, if the activity to be started has an action name defined within <intent-filter> in the [AndroidManifest.xml](https://www.mindstick.com/interview/12768/what-is-the-androidmanifest-xml) we can use to call the activity too.

\
**There are two ways to open a new activity.**

· Using the "startactivity(Intent intent)" method

· Using "startActivityforResult(Intent intent, requestCode requestCode)" method.

When we open a folder or file using another for importing or save some data then we use **startActivityforResult()**,Because by calling this second Activity the open is only for some time and provides some output. Whereas startActivity() opens a separate new activity.

**Pre-Requisites:**

1. Ellipse SDK

2. Android SDK

3. ADT plugin

Or [Android Studio](https://www.mindstick.com/blog/10942/android-studio-vs-eclipse) and a compatible version of JAVA SDK

Install and configure the above utilities.

Now create a new Android project namely “**MindStickActivity**”.

**Implementation objective:**

· How to call “start” another activity within the same application?

· How to prepare the Intent object using class name or action name?

· How to send data from one [activity to another activity](https://answers.mindstick.com/qa/35040/how-to-pass-data-one-activity-to-another-activity) using **putExtra() method**

· How to [receive data](https://www.mindstick.com/interview/22931/will-these-phones-have-world-phone-capabilities-with-the-ability-to-make-calls-receive-data-in-other-countries-or-will-they-be-able-to-use-google-voice-to-make-calls) from sending activity and use it.

**Creating GUI’s for the Activities:**

Define the layout for the first “main” activity and the second “new” activity to be started.

We need

1. For Main activity :A Linear Layout consist of a [Text View](https://www.mindstick.com/forum/33384/detail-text-view-of-cell-not-showing)(Indicating about current Activity) and a button which will help us trigger the new activity

2. For New activity :A Linear Layout consist of a Text View(Indicating about current Activity) and another Text View which will show the data send by the main Activity

Open **"MindStickActivity/res/layout/activity_main.xml"** from package explorer and update it as in the following code:

```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <TextView
        android:text="Main Activity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button_start"
        android:text="Start New Activity"/>
</LinearLayout>
```

Now navigate to "**MindStickActivity/res/layout/activity_new.xml**" from package explorer. Open activity_new.xml

```
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.example.msclient010.mindstickactivity.NewActivity">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/newText"        android:text="We are in new Activity"/>    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/data_text"        />
</LinearLayout>
```

**Creating new Activity:**

Create a **new Activity** Java file named "NewActivity.java” and the following code will be generated in it:

```
package com.example.msclient010.mindstickactivity;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.view.Menu;import android.view.MenuItem;import android.widget.TextView;
public class NewActivity extends ActionBarActivity {
    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_new);
    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_new, menu);
        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();
        //noinspection SimplifiableIfStatement        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}
```

**Updating Android [Manifest file](https://www.mindstick.com/forum/158884/what-is-the-purpose-of-the-android-manifest-file):**

Now open **AndriodManifest.xml** and update following code in it:

```
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.msclient010.mindstickactivity" >
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".NewActivity"
            android:label="@string/title_activity_new" >
        </activity>
    </application>
</manifest>
```

## Implementing Main Activity class:

Navigate to **MainActivity** in src folder from package explorer. Open MainActivity and update this code in it:

```
package com.example.msclient010.mindstickactivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //getting a reference to the Start New Activity button
        Button btn = (Button)findViewById(R.id.button_start);
        //making the button clickable
        btn.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //creating an intent instance to start a new activity
                Intent i= new Intent(MainActivity.this,NewActivity.class);
                //passing data from main activity to new activity
                i.putExtra("Text","Data from Main activity to New Activity");
                //Starting New activity
                startActivity(i);
            }
        });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
```

\

**Implementing New Activity class:**

Navigate to **NewActivity in src** folder from package explorer. Open NewActivity and update this code in it:

```
package com.example.msclient010.mindstickactivity;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.view.Menu;import android.view.MenuItem;import android.widget.TextView;
public class NewActivity extends ActionBarActivity {
    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_new);        //getting reference of data_text Text view        TextView dataTxt = (TextView)findViewById(R.id.data_text);
       // Getting String data from the main activity        String data = getIntent().getStringExtra("Text");        //setting the string data from main activity to the textView        dataTxt.setText(data);
    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.menu_new, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();
        //noinspection SimplifiableIfStatement        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}
```

**Running the Application:**

Just hit on “Run” button and our app will be launched and the main activity will be displayed:

![Start a new activity within an Activity in Android](https://www.mindstick.com/mindstickarticle/8551a7a3-9e52-445d-a2f9-cd2ee0f5b864/images/b3bc1b3b-de3a-49aa-91cd-5bc55731023a.png)

Click on “Start New Activity” button and the new activity will be started and will be displayed on screen.

Notice the statement in the second line, it is the string which is send by the main activity and set to text view in the new activity.

![Start a new activity within an Activity in Android](https://www.mindstick.com/mindstickarticle/8551a7a3-9e52-445d-a2f9-cd2ee0f5b864/images/12a42865-5828-4bc7-9b95-a5c8bf09f59d.png)

Next, we are going to implement an endless list view adapter in **Endless List view Adapter in Android**

Thanks for reading this post.

Happy Coding!! J

---

Original Source: https://www.mindstick.com/articles/1569/start-a-new-activity-within-an-activity-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
