Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi there

I am new in Android development and I have been trying to launch activities from another. Let say from "Main Activity" I launch the "Home Activity" by clicking a button. Then from "Home Activity", I want to be able to launch 5 different activities based on whichever button is clicked. I have tried many resource online and none seems to work for me.
From Main to Home it is working fine but from Home to the other activities, nothing is happening.

Please assist.
PS. Below is my sample code:

MainActivity.java

package com.example.unisalabapp;

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

public class MainActivity extends Activity implements OnClickListener {

Button btnLogin;

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

btnLogin = (Button) findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(this);
}

@Override
public void onClick(View v) {

Intent l = new Intent(getApplicationContext(), Home.class);
startActivity(l);
}

}

HomeLauncher.java

package com.example.unisalabapp;

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

public class HomeLauncher extends Activity implements OnClickListener {

Button btnBooking;

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

//setup button listener
btnBooking = (Button) findViewById(R.id.btnBooking) ;
btnBooking.setOnClickListener(this);
}
@Override
public void onClick(View v) {

Intent b = new Intent(getApplicationContext(), ComputerBooking.class);
startActivity(b);
}
}

one of the five other activities:

package com.example.unisalabapp;

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

public class ComputerBooking extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.computer_booking);

//setup button listener
Button startButton = (Button) findViewById(R.id.btnBack);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
finish();
}
});
}

}

The Manifest:


<manifest xmlns:android="http://schemas.android.com/apk/res/android">
package="com.example.unisalabapp"
android:versionCode="1"
android:versionName="1.0" >

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

<application>
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

<activity>
android:name="com.example.unisalabapp.MainActivity"
android:launchMode="singleTop"
android:stateNotNeeded="true"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN">
<category android:name="android.intent.category.LAUNCHER">


<activity>
android:name="com.example.unisalabapp.HomeLauncher"
android:launchMode="singleTop"
android:stateNotNeeded="true"
android:label="@string/app_name">
<intent-filter>
<action android:name="com.example.unisalabapp.HOME">
<action android:name="android.intent.category.DEFAULT">
<category android:name="android.intent.category.LAUNCHER">



<activity>
android:name="com.example.unisalabapp.computer_booking"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.COMPUTER_BOOKING">
<!-- <action android:name="com.example.unisalabapp.COMPUTER_BOOKING"> -->
<action android:name="android.intent.category.DEFAULT">




Posted

1 solution

On the home activity, the onclick event handler is being shared by multiple buttons, you will have to identify the button that trigger it in order to launch the correct activity. Check this out: http://www.tutorialsbuzz.com/2014/02/android-group-onclick-listener-multiple-button.html[^]
Another way is to attach a distinct event handler to the onclick attribute of each button on the xml layout of home activity, check this out: Android UI Layouts and Controls[^]
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900