• Techiax

Tutorial - Creating, Using and transferring data between Android Activities

Android activity tutorial

This tutorial covers the aspect of creating, using and transferring data between Android Activities. First let's see what is an activity.
Activity is the first important component you should learn to get into the area of Android App Development. For a short and better understanding, An Activity is a single screen with different components like buttons, text views, etc. you see in an android app. You would have seen in many android apps that when you interact with the screen like pressing buttons or entering text then the screen would change into a new one, this is because you are moving from one activity to another activity, each screen corresponds to one activity.

Create a new application and activity:


First create a new project in Android Studio
  • File->New Project
  • Name your application and click next.
  • Click next until you see a window choose the activity type. Choose Blank Activity from there and leave default activity name which is MainActivity.
This is your MainActivity and this is the first activity which will load when you open your application. And now to create another activity.

  • File->New->Activity->Blank Activity
  • Choose name a MainActivity2

Each activity you add, must be given references in your AndroidManifest.xml file. But don't worry, your IDE will take of it for you.

Starting new Activity:

You are having two activities now. The MainActivity is the first activity of your app and it load by default. Now you need to start our second Activity(MainActivity2) from the MainActivity. To do this first need to create an intent for the MainActivity2 and start the Activity.

The format is,


 Intent intent_name=new Intent(current_Activity.this,destination_Activity.class);
 startActivity(intent_name);

These two lines of code will bring your new Activity to the screen(foreground) and pushes the MainActivity to the background. This lines should be added to current_Activity.java file. An example is provided at the end of this tutorial with all factors.
Intent is a bigger topic, but for short you could take it as a way of communication between different components in the android application. But mostly it is used start components like this.

Transferring data between Activities:

Transferring data between Activities is very important as for bigger apps the destination activity should be changed according to the different user interface. For example, if you are using a shopping app, there would be first large categories of items and when you click on different items different details of each item come up. Making a different activity for each item is not a feasible solution. So the app would be designed to transfer your selection to the next activity and this activity displays the details based on data given from first activity.

To transfer data, you need to send data from current activity and receive the data at another activity.

To send data:


 intent_name.putExtra(key,value);


It should be added before you start the activity. You could send many types of values like integer, float, string, etc.

To receive data:


 String name=getIntent().getStringExtra(key);


This is for receive string datatype. For int you could use getIntExtra(key,defaultValue); and there are methods for each type.

To check all the methods click here.

Example Code:

AndroidManifest.xml


 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.techiax" >

    <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=".MainActivity2"
            android:label="@string/title_activity_main_activity2"
            android:parentActivityName=".MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.abdulrahuman.techaix.MainActivity" />
        </activity>
    </application>

 </manifest>


activity_main.xml:


 <RelativeLayout 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"
                tools:context=".MainActivity">

    <TextView
        android:text="Techiax"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/techiax"
        android:textIsSelectable="true"
        android:textSize="40sp"
        android:textStyle="bold"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Activity tutorial"
        android:id="@+id/textView"
        android:layout_marginTop="102dp"
        android:textSize="30sp"
        android:layout_below="@+id/techiax"
        android:layout_centerHorizontal="true"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:id="@+id/editText"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send"
        android:id="@+id/button"
        android:layout_below="@+id/editText"
        android:layout_centerHorizontal="true"
        android:textSize="30sp"
        />
 </RelativeLayout>


MainActivity.java


 package com.example.techaix;

 import android.content.Intent;
 import android.support.v7.app.ActionBarActivity;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.Button;
 import android.widget.EditText;


 public class MainActivity extends ActionBarActivity {

    EditText editText;
    Button button;
    Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText=(EditText)findViewById(R.id.editText);
        button=(Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String value=String.valueOf(editText.getText());
                intent=new Intent(MainActivity.this,MainActivity2.class);
                intent.putExtra("key",value);
                startActivity(intent);

            }
        });

    }

 }

activity_main_activity2.xml:


 <RelativeLayout 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"
                tools:context="com.example.abdulrahuman.techaix.MainActivity2">

    <TextView
        android:text="Techiax"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:id="@+id/textView2"
        android:textIsSelectable="true"
        android:textSize="40sp"
        android:textStyle="bold"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Activity tutorial"
        android:id="@+id/textView3"
        android:textSize="30sp"
        android:layout_below="@+id/textView2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="128dp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:id="@+id/textView4"
        android:layout_below="@+id/textView3"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="79dp"
        android:textSize="20sp"
        />
 </RelativeLayout>

MainActivity2.java


 package com.example.techaix;

 import android.support.v7.app.ActionBarActivity;
 import android.os.Bundle;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.widget.TextView;


 public class MainActivity2 extends ActionBarActivity {

    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_activity2);
        textView=(TextView)findViewById(R.id.textView4);
        String value=getIntent().getStringExtra("key");
        textView.setText(value);
    }

 }


Output:

Type a text and click on send button.

output3output1output2




If you have any queries please let us know in comments below.

Also Read: Tutorial - C programming - Decision Statements


Please share your views and thoughts. It will help us do more.






Author:

I am an enthusiastic person striving to gather as much information as possible to achieve higher heights and to share the information to all others to make them also reach the stars.
"People who are crazy enough to think they can change the world are the ones who do"
-Steve Jobs.

Previous Post
Next Post