How to switch between activities?

By using intents.

An Intent is a way to move between activities (screens) in the app.

You could send information with your Intent to use in the other activity, and you could request to receive information from that same activity.

In this tutorial I will demonstrate, how you could simply go to another activity.

First lets create the xml layouts for your activities.

At this location “res\layout\” create the xml file “first_choice.xml” and copy this.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/first_toolbar"
        android:layout_width="match_parent"
        android:layout_height="58dp"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:titleTextColor="#ffffff">
    </android.support.v7.widget.Toolbar>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Nothing to see here"
        />
    
</RelativeLayout>

Now that we have the xml file created, lets create the activity.

In your package under the Java folder create the activity “firstChoice.java” and copy the following code.

package com.thewannabeprogrammer.wbpuselessapp;

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

public class FirstChoice extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_choice);

        Toolbar myToolbar = (Toolbar) findViewById(R.id.first_toolbar);
        setSupportActionBar(myToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

You will notice that the FirstChoice.Java class is fairly empty. All I am doing here is inflating the xml layout “first_choice” and I added the toolbar like in the MainActivity.java.

Just as a note, the getSupportActionBar().setDisplayHomeAsUpEnabled(true) sets the little back arrow you see in the Toolbar.

Now, its important to remember that every new activity you create you must declare it in the manifest. Lets go see what the manifest looks like.

Go to app\manifests\AndroidManifest.xml. in here you will have to put the activities that you want to create. So lets add our new activity right now. Copy this into your manifest.

...
<activity android:name=".FirstChoice">
<meta-data
    android:name="android.support.PARENT_ACTIVITY"
    android:value="com.thewannabeprogrammer.wbpuselessapp.MainActivity"/>
</activity> 
...

Your AndroidManifest.xml file should now look like this.

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:launchMode="singleTop"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity"
            android:parentActivityName="com.thewannabeprogrammer.wbpuselessapp.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".FirstChoice">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.thewannabeprogrammer.wbpuselessapp.MainActivity"/>
        </activity>
    </application>

</manifest>

In the FirstChoice <activity> tag, you will notice that we declare PARENT_ACTIVITY is the MainActivity. So when you click on the back arrow it takes you back to the mainpage.

Now in the MainActivity.java add the new code below where I marked the spot with “add new code her”

….drawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, drawerListItems));

…ADD NEW CODE HERE….

drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_open, R.string.drawer_close) {
public void onDrawerClosed(View v) {…..

drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        switch (position){
            case 1:
                Intent i1 = new Intent(parent.getContext(), FirstChoice.class);
                startActivity(i1);
                break;
            case 2:
                // do Nothing here for now.
                break;
            case 3:
                // do Nothing here for now
                break;
        }

    }
});

When you click on an item in the drawer menu you will be able to handle the click events with the onItemClickListener.

We also created a switch statement to handle which item you clicked on.

So for Case 1, its the first item on the top of the list. In here we are using an Intent to go to the FirstChoice activity.

In the FirstChoice activity when you click on the back arrow in the menu you will go back to the MainActivity.

Run your app and test it. See the screen shots below for what to expect.

intent1 click on the first Item “this does nothing” intent1-firstchoice you should go to the FirstChoice page, and if you click on the back arrow, you will go back to the MainActivity page.

Next I will show you how to send data using an Intent.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s