Start Another Activity

Share this

In this post we will see how to start new activity.
In android app many screens/Activities are used.
so we have to navigate to another activity or go back to previous activity.
we will see how to do this.open android studio.

1. Create a new android project give name as Activity.

2. I used package path as com.androidcodr.activity

3. Select minimum api version 14.

4. Add empty activity from next screen.

5. on next screen click finish.

6. on activity_main.xml
change text of textview to “First Activity ”
size to “24sp”

<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="First Activity"
        android:gravity="center_horizontal"
        android:textSize="24sp" />

7. add one button at center of screen
change text property to Login
add onClick property to loginnow

 

 <Button
        android:text="Login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/button"
        android:onClick="loginnow" />

now we will add second activity to our app.

8. on left on project explorer
right click on app.

New–> Activity –> Empty Activity

Add Second Activity to app

9. set activity name as SecondActivity

Start a New Activity

wait few moment while Android Studio adding activity
this will add
a java file as SecondActivity.java
a layout xml as activity_second.xml
and it will automatically define our new activity in AndroidManifest file

define activity in AndroidManifest

10. add a textView in second activity layout file
activity_second.xml

Start a New Activity

 

 <TextView
        android:text="Second Activity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/textView"
        android:gravity="center_horizontal"
        android:textSize="24sp" />

11. now lets add code to start our second activity from first activity
we will add method thats added in button onclick property

  public void loginnow(View view){

    }

12. to start second activity we will use Intent

An intent is an abstract description of an operation to be performed.
It can be used with startActivity to launch an Activity.

 

   Intent i = new Intent(this,SecondActivity.class);
        startActivity(i);

while starting new Intent pass first parameter this as Context
and name of class file of new activity here SecondActivity.class

then call startActivity()
and pass Intent i

13. run the app

click on login button

our second activity is now opening.

we can go back to previous activity by pressing back button.

we can also add back arrow on action bar.

Action bar Home Back button

Lets see how to add

14. On SecondActivity.class
inside onCreate method.

get actionbar and set home up enable

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

15. its displayed now but not functioning because we have not added code for it.

16. Override method onOptionsItemSelected type ‘onOp’ press ctrl+space android studio will give you suggestion press enter method will be added.

OnOptionsItemSelected method

 

   @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

get the itemId of MenuItem
check if it is equal to home back button in action bar
android.R.id.home
call finish(); method to close current activity.

here is full code
activity_main.xml

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.androidcodr.activity.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="First Activity"
        android:gravity="center_horizontal"
        android:textSize="24sp" />

    <Button
        android:text="Login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/button"
        android:onClick="loginnow" />
</RelativeLayout>

MainActivity.class

package com.androidcodr.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void loginnow(View view){
        Intent i = new Intent(this,SecondActivity.class);
        startActivity(i);
    }
}

activity_second.xml

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_second"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.androidcodr.activity.SecondActivity">

    <TextView
        android:text="Second Activity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/textView"
        android:gravity="center_horizontal"
        android:textSize="24sp" />
</RelativeLayout>

SecondActivity.class

package com.androidcodr.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

 

 

If you Like post. Share with your Friends.
Subscribe us.
Follow us on Facebook Twitter and Google+


Share this

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *