Using Intent in an Android application to show another activity

AndroidAndroid IntentAndroid Activity

Android Problem Overview


In my Android application, I have two activity classes. I have a button on the first one and I want to show the second when it is clicked, but I get an error. Here are the classes:

public class FirstActivity extends Activity {
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button orderButton = (Button)findViewById(R.id.order);
    
    orderButton.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View view) {
        Intent intent = new Intent(FirstActivity.this, OrderScreen.class);
        startActivity(intent);
      }
      
    });
  }
}

The second class that should show when the button is clicked, but never does:

public class OrderScreen extends Activity {

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

    Button orderButton = (Button) findViewById(R.id.end);

    orderButton.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View view) {
        finish();
      }

    });
  }
}

How do I create a button that will show the second activity?

Android Solutions


Solution 1 - Android

The issue was the OrderScreen Activity wasn't added to the AndroidManifest.xml. Once I added that as an application node, it worked properly.

<activity android:name=".OrderScreen" />

Solution 2 - Android

Add this line to your AndroidManifest.xml:

<activity android:name=".OrderScreen" /> 

Solution 3 - Android

----FirstActivity.java-----

    package com.mindscripts.eid;
    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;

public class FirstActivity extends Activity {

protected void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onCreate(savedInstanceState);
	setContentView(R.layout.main);
	Button orderButton = (Button) findViewById(R.id.order);
	orderButton.setOnClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			Intent intent = new Intent(FirstActivity.this,OrderScreen.class);
			startActivity(intent);
		}
	});
	
 }
}
 

---OrderScreen.java---

    package com.mindscripts.eid;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;



    public class OrderScreen extends Activity {
@Override



protected void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onCreate(savedInstanceState);
	setContentView(R.layout.second_class);
	Button orderButton = (Button) findViewById(R.id.end);
	orderButton.setOnClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			finish();
		}
	});
	
 }
}

---AndroidManifest.xml----

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.mindscripts.eid"
  android:versionCode="1"
  android:versionName="1.0">


<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".FirstActivity"
              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=".OrderScreen"></activity>
</application>

Solution 4 - Android

Use this code:

Intent intent=new Intent(context,SecondActivty.class);
startActivity(intent);
finish();

context: refer to current activity context,

please make sure that you have added activity in android manifest file.

Following code for adding activity in android manifest file

<Activity name=".SecondActivity">
</Activity>

Solution 5 - Android

<activity android:name="[packagename optional].ActivityClassName"></activity>

Simply adding the activity which we want to switch to should be placed in the manifest file

Solution 6 - Android

When you create any activity in android file you have to specify it in AndroidManifest.xml like

http://schemas.android.com/apk/res/android" package="com.demo.creativity" android:versionCode="1" android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".MyCreativityActivity"
        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=".OrderScreen"></activity>
    
    
</application>

Solution 7 - Android

b1 = (Button) findViewById(R.id.click_me);
		b1.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
		
				Intent i = new Intent(MainActivity.this, SecondActivity.class);
				startActivity(i);

			}
		});

Solution 8 - Android

add the activity in your manifest file

<activity android:name=".OrderScreen" />

Solution 9 - Android

In the Manifest

<activity android:name=".OrderScreen" />

In the Java Code where you have to place intent code

startActivity(new Intent(CurrentActivity.this, OrderScreen.class);

Solution 10 - Android

you can use the context of the view that did the calling. Example:

Button orderButton = (Button)findViewById(R.id.order);

orderButton.setOnClickListener(new View.OnClickListener() {

  @Override
  public void onClick(View view) {
    Intent intent = new Intent(/*FirstActivity.this*/ view.getContext(), OrderScreen.class);
    startActivity(intent);
  }

});

Solution 11 - Android

Intent i = new Intent("com.Android.SubActivity");
startActivity(i);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTai SquaredView Question on Stackoverflow
Solution 1 - AndroidTai SquaredView Answer on Stackoverflow
Solution 2 - Androiduser106011View Answer on Stackoverflow
Solution 3 - AndroidSunil ChavanView Answer on Stackoverflow
Solution 4 - AndroidMaheshView Answer on Stackoverflow
Solution 5 - Androidjava devView Answer on Stackoverflow
Solution 6 - AndroidAndroid-iPhone-rahulView Answer on Stackoverflow
Solution 7 - AndroidHiren PatelView Answer on Stackoverflow
Solution 8 - AndroidNeal AhluvaliaView Answer on Stackoverflow
Solution 9 - AndroidNileshView Answer on Stackoverflow
Solution 10 - AndroidBrunoView Answer on Stackoverflow
Solution 11 - AndroidNdupzaView Answer on Stackoverflow