Home > DeveloperSection > Articles > Start a new activity within an Activity in Android

Start a new activity within an Activity in Android


Android Android  Android Activity  Android Intent 
Ratings:
0 Comment(s)
 1232  View(s)
Rate this:

Start a new activity within an Activity in Android


Previously , we learn how to implement contextual menus on Long press in android : Contextual Menu 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” you want to start. Alternatively, if the activity to be started has an action name defined within <intent-filter> in 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 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 using putExtra() method

·         How to receive data 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(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:

 

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:

 

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.

 

 

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

 


Don't want to miss updates? Please click the below button!

Follow MindStick