1. Download our Official Android App: Forums for Android!

Apps Switching activities

Discussion in 'Android Development' started by Yathushan, Feb 14, 2011.

  1. Yathushan

    Yathushan Well-Known Member
    Thread Starter
    Rank:
    None
    Points:
    36
    Posts:
    205
    Joined:
    Jan 3, 2010

    Jan 3, 2010
    205
    7
    36
    London
    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 (Text):
    1.  
    2. package com.transport.assistant;
    3.  
    4. import android.app.Activity;
    5. import android.content.Intent;
    6. import android.os.Bundle;
    7. import android.view.View;
    8. import android.view.View.OnClickListener;
    9. import android.widget.Button;
    10.  
    11. public class mainmenu extends Activity {
    12.     /** Called when activity is initially created. */
    13.     @Override
    14.     public void onCreate(Bundle savedInstanceState) {
    15.         super.onCreate(savedInstanceState);
    16.         setContentView(R.layout.main);
    17.        
    18.          // click-handlers for buttons
    19.         Button underground = (Button) findViewById(R.id.ButtonM1);
    20.         underground.setOnClickListener(new View.OnClickListener() {
    21.            
    22.             public void onClick(View v) {
    23.                 Intent myIntent = new Intent(view.getContext(), underground.class);
    24.                         startActivityForResult(myIntent, 0);
    25.             }
    26.            
    27.         });
    28.     }
    29. }
    30.  
    main.xml
    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    4.     android:orientation="vertical"
    5.     android:layout_width="fill_parent"
    6.     android:layout_height="fill_parent">
    7.    
    8.     <TextView
    9.     android:id="@+id/textView1"
    10.     android:layout_height="wrap_content"
    11.     android:layout_width="match_parent"
    12.     android:text="@string/app_name">
    13.     </TextView>
    14.    
    15.     <Button
    16.     android:id="@+id/ButtonM1"
    17.     android:layout_width="match_parent"
    18.     android:layout_height="wrap_content"
    19.     android:text="@string/Underground">
    20.     </Button>
    21.    
    22.     <Button
    23.     android:id="@+id/ButtonM2"
    24.     android:layout_width="match_parent"
    25.     android:layout_height="wrap_content"
    26.     android:text="@string/Bus">
    27.     </Button>
    28.    
    29.     <Button
    30.     android:id="@+id/ButtonM3"
    31.     android:layout_width="match_parent"
    32.     android:layout_height="wrap_content"
    33.     android:text="@string/oyster_locations">
    34.     </Button>
    35.    
    36.     <Button android:id="@+id/ButtonM4"
    37.     android:layout_width="match_parent"
    38.     android:layout_height="wrap_content"
    39.     android:text="@string/plan_journey">
    40.     </Button>
    41.    
    42.     <Button
    43.     android:id="@+id/ButtonM5"
    44.     android:layout_width="match_parent"
    45.     android:layout_height="wrap_content"
    46.     android:text="@string/Settings">
    47.     </Button>
    48.    
    49. </LinearLayout>
    50.  
    Any advise would be much appreciated!!
     

    Advertisement

  2. Tapirboy

    Tapirboy Member
    Rank:
    None
    Points:
    15
    Posts:
    42
    Joined:
    Feb 10, 2011

    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 (Text):
    1.  
    2. Intent  i = new Intent(mainmenu.this, OtherActivity.class);
    3.  
     
  3. Yathushan

    Yathushan Well-Known Member
    Thread Starter
    Rank:
    None
    Points:
    36
    Posts:
    205
    Joined:
    Jan 3, 2010

    Jan 3, 2010
    205
    7
    36
    London
    Hey I tried this. It does not work. I have just modified the code but it still doesn't bring up the next screen.
     
  4. cr5315

    cr5315 Android Enthusiast
    Rank:
    None
    Points:
    68
    Posts:
    434
    Joined:
    Jul 23, 2010

    Jul 23, 2010
    434
    158
    68
    Narnia
    Instead of
    Code (Text):
    1. startActivityForResult(myIntent, 0);
    try
    Code (Text):
    1. startActivity(myIntent);
    Along with the change Tapirboy had for the line before this one.

    Code (Text):
    1. Intent  myIntent = new Intent(mainmenu.this, OtherActivity.class);
    2. startActivity(myIntent);
     
  5. Yathushan

    Yathushan Well-Known Member
    Thread Starter
    Rank:
    None
    Points:
    36
    Posts:
    205
    Joined:
    Jan 3, 2010

    Jan 3, 2010
    205
    7
    36
    London
    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.
     
  6. cr5315

    cr5315 Android Enthusiast
    Rank:
    None
    Points:
    68
    Posts:
    434
    Joined:
    Jul 23, 2010

    Jul 23, 2010
    434
    158
    68
    Narnia
    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.
     
  7. Yathushan

    Yathushan Well-Known Member
    Thread Starter
    Rank:
    None
    Points:
    36
    Posts:
    205
    Joined:
    Jan 3, 2010

    Jan 3, 2010
    205
    7
    36
    London
    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?
     

Share This Page

Loading...