且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从Android中的一个活动启动活动

更新时间:2023-01-23 21:11:35

在家庭活动中,onclick事件处理程序由多个按钮共享,你必须识别触发它的按钮才能启动正确的活动。看看这个: http://www.tutorialsbuzz.com/2014/ 02 / android-group-onclick-listener-multiple-button.html [ ^ ]

另一种方法是将独特的事件处理程序附加到xml布局上每个按钮的onclick属性上家庭活动,请查看: Android UI布局和控件 [ ^ ]

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">




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[^]