• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Switching activities

Yathushan

Well-Known Member
Hi all,
I have just started on my first app. I was just wondering how can I move from one activity to another.
I have used onClickListeners. It does not seem to work for me even though I have pretty much followed every tutorial I can find step by step.

mainmenu.java
Code:
package com.transport.assistant;

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 mainmenu extends Activity {
	/** Called when activity is initially created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		 // click-handlers for buttons
		Button underground = (Button) findViewById(R.id.ButtonM1);
		underground.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				Intent myIntent = new Intent(view.getContext(), underground.class);
                		startActivityForResult(myIntent, 0);
			}
			
		});
	}
}

main.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
    <TextView
    android:id="@+id/textView1"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:text="@string/app_name">
    </TextView>
    
    <Button
    android:id="@+id/ButtonM1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/Underground">
    </Button>
    
    <Button
    android:id="@+id/ButtonM2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/Bus">
    </Button>
    
    <Button
    android:id="@+id/ButtonM3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/oyster_locations">
    </Button>
    
    <Button android:id="@+id/ButtonM4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/plan_journey">
    </Button>
    
    <Button
    android:id="@+id/ButtonM5"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/Settings">
    </Button>
    
</LinearLayout>

Any advise would be much appreciated!!
 
You are using the intent wrong, the second argument, Class, should be a class of the Activity you want to start, like OtherActivity.class.

Code:
Intent  i = new Intent(mainmenu.this, OtherActivity.class);
 
Hey I tried this. It does not work. I have just modified the code but it still doesn't bring up the next screen.
 
Instead of
Code:
startActivityForResult(myIntent, 0);
try
Code:
startActivity(myIntent);

Along with the change Tapirboy had for the line before this one.

Code:
Intent  myIntent = new Intent(mainmenu.this, OtherActivity.class);
startActivity(myIntent);
 
Still does not seem to work.

I just want to clarify. These are initial menus. So that once you start up that app it shows main.xml . Then you click on buttonM1 which takes you to the next menu set, underground.xml . I was wondering whether the second screen needed specific coding as well.
 
There shouldn't be any special code in the next activity, although, in the code you posted, you were launching for result, which is different than just launching the activity.
I have made many apps that launch new activities and the code I provided works just fine for me. I'm going to try to build a replica of what you've provided so far and see if I can get it working.
 
I was just wondering is there any other way for me to display two screens as in one view and then proceed into a brand new view after they select a button?
 
Back
Top Bottom