Thursday, February 5, 2015

How to switch between Activities in Android

In Android user interface is displayed through an activity. In Android app development you have to face situations where you need to switch between one Activity (Screen) to another. In this tutorial we will be discussing about switching between one Activity to another and sending data between the activities.Here we explain with the help of stable android IDE Android Studio.

For this purpose we use a class Intent. And uses the methods of intent class for sending data between activities.
To open new activity we use startActivity() method
Intent i = new Intent(getApplicationContext(), SecondScreen.class);
StartActivity(i);

To send parameter to newly created activity putExtra() method will be used.
i.putExtra("key", "value");
example
i.putExtra("Name","xyz");

To receive parameters on newly created activity getStringExtra()  method will be used.
Intent i = getIntent();
i.getStringExtra("key");
String name = i.getStringExtra("Name");

So now we have all the code snippets related to activities. In this tutorial i created two xml layouts(First.xml, Second.xml) and two Acvities (First.java,Second.java). In which we are switching between them.

Add entery in  AndroidManifest.xml
To run our application you should enter your new activity in AndroidManifest.xml file.A new activity between <application> tag.
<activity android:name=".NewActivityClassName"></activity>



The code for First.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class First extends Activity implements OnClickListener {
     Button b1;
     EditText e1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one);
        b1=(Button)findViewById(R.id.button1);
e1=(EditText)findViewById(R.id.editText1);
        b1.setOnClickListener(this);
}
      @Override
     public void onClick(View arg0) {
           // TODO Auto-generated method stub
           Intent in=new Intent(this,Second.class);
           in.putExtra("ack",e1.getText().toString());
           startActivity(in);}}

The code for Second.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Second extends Activity implements OnClickListener {
     Button b2;
     EditText e2;
    @Override
    protected void onCreate(BundlesavedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.two);
        b2=(Button)findViewById(R.id.button2);
e2=(EditText)findViewById(R.id.editText2);
        b2.setOnClickListener(this);
        Intent i=getIntent();
String s=i.getStringExtra("ack");
        e2.setText(s);
 }
     @Override
     public void onClick(View arg0) {
           // TODO Auto-generated method stub
     finish();
     }
}

The code for first.xml
<LinearLayout 
xmlns:android="http://schemas.android.com/app/res/android"  
        xmlns:tools="http://schemas.android.com/tools"
     android:id="@+id/container"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:gravity="fill_vertical"
     android:orientation="vertical" >
 <EditText android:id="@+id/editText1"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:ems="10">
  <requestFocus /> 
  </EditText>
  <Button android:id="@+id/button1"
          android:layout_width="118dp"
          android:layout_height="102dp" 
          android:text="Button" /> 
  </LinearLayout>
The code for second.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        xmlns:tools="http://schemas.android.com/tools"
     android:id="@+id/container"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:gravity="fill_vertical"
     android:orientation="vertical" >
 <EditText android:id="@+id/editText2"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:ems="10">
  <requestFocus />
  </EditText>
  <Button android:id="@+id/button2"
          android:layout_width="118dp"
          android:layout_height="102dp" 
          android:text="Button" />
  </LinearLayout>

The mainfiest.xml
<?xml version="1.0" encoding="utf-8" 
?> 
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.intent"
    android:versionCode="1"
    android:versionName="1.0">
  <uses-sdk 
       android:minSdkVersion="15"
       android:targetSdkVersion="21" /> 
 <application android:allowBackup="true"
               android:icon="@drawable/ic_launcher" 
               android:label="@string/app_name"
               android:theme="@style/AppTheme">
 <activity android:name="com.example.intent.first"
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="com.example.intent.Second"
l̥android:label="@string/app_name">
 <intent-filter
  <category android:name="android.intent.category.DEFAULT" /> 
  </intent-filter>
  </activity>
  </application>

  </manifest>

No comments:

Post a Comment