How to navigate from one page to another on android?(Total:3 pages)

Go To StackoverFlow.com

2

I am new to Android development and I have a question regarding how to navigate from one page to another. Actually,what I want to do is this: Open app,first page appears,it says "Hello I am activity 1".Then there is a button which says "Next",you press next then you navigate to second page where it says "Hello I am activity 2".In this page there are two buttons,first says "Previous" which takes you back to page 1 and second says "Next" which takes you to page 3. Basically,this is where I am stuck,page 1 and 2 works fine,both next and previous buttons but I can't navigate to 3rd page when I press the "next" button from page 2. I have uploaded my source code here so you guys can download it and import it to Eclipse in order to see exactly what I have done.

Click here to download.

Would be glad if someone could help mates, Thanks in advance.

OK,code is shown here,I have created 3 activities which I also registered at Manifest and I also have created 3 layouts for these 3 activities.

Activity 1

import android.app.Activity;<br>
import android.content.Intent;<br>
import android.os.Bundle;<br>
import android.view.View;<br>
import android.widget.Button;<br>

public class Activity1 extends Activity {

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

        Button next = (Button) findViewById(R.id.Button01);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), Activity2.class);
                startActivityForResult(myIntent, 0);
            }

        });
    }
}

Activity2

import android.app.Activity;<br>
import android.content.Intent;<br>
import android.os.Bundle;<br>
import android.view.View;<br>
import android.widget.Button;<br>

public class Activity2 extends Activity {

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);

        Button next = (Button) findViewById(R.id.Button02);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();
            }

        });
    }


public void onCreate1(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     setContentView(R.layout.main2);

    Button next = (Button) findViewById(R.id.Button04);
    next.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent myIntent1 = new Intent(view.getContext(), Activity3.class);
            startActivityForResult(myIntent1, 0);
        }

    });
}
}

Activity3

import android.app.Activity;<br>
import android.content.Intent;<br>
import android.os.Bundle;<br>
import android.view.View;<br>
import android.widget.Button;<br>

public class Activity3 extends Activity {

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main3);

        Button next = (Button) findViewById(R.id.Button04);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();
            }

        });
    }
}

main.xml

  <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#000000"
    android:text="This is Activity 1" />

       <Button android:text="Next"
        android:id="@+id/Button01"
        android:layout_width="250px"
            android:textSize="18px"
        android:layout_height="55px">
    </Button>    

</LinearLayout>

main2.xml

    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#000000"
    android:text="This is Activity 2" />

       <Button android:text="Previous"
        android:id="@+id/Button02"
        android:layout_width="250px"
            android:textSize="18px"
        android:layout_height="55px">
    </Button>



       <Button
           android:layout_width="162dp"
           android:layout_height="34dp"
           android:text="Next"
           android:id="@+id/Button04"
           android:textSize="18px" />

</LinearLayout>

main3.xml

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="This is Activity 3" />

I have also registered my activities in Manifest.

2012-04-05 21:18
by Μηνάς Γκέργες
you should post your code through here, I personally am not a big fan of downloading zipped up files from people I don't know - testingtester 2012-04-05 21:29
very easy you must use intents to navigate between activities - haythem souissi 2012-04-05 21:29


2

Step #1: Replace startActivityForResult() with startActivity() in Activity1

Step #2: Remove your current code in the onClick() from Activity2 and replace it with a call to startActivity() to start Activity3

Step #3: Completely rewrite Activity3, as either it will not compile or it will crash at runtime, as you are referring to a widget that does not exist (Button04)

2012-04-05 22:35
by CommonsWare


1

I'm not sure if it't too late but if you still need a clear answer then:

You will need to start activities. There isn't a DIRECT page navigation code, but instead there is a start Activity. By starting an activity you are able to go to another page because the page is related to the activity.

Intent myIntent = new Intent(Enter_Your_Current_Activity.this, Enter_The_Activity_You_Want_To_Navigate_To.class);
startActivity(myIntent);

Hope this helps!

p.s What I use in my code.

2014-06-22 00:56
by Zer0


0

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

public class Activity1 extends Activity {

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

        Button next = (Button) findViewById(R.id.Button01);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), Activity2.class);
                startActivityForResult(myIntent, 0);
            }

        });
    }
}
2013-02-19 08:38
by sivaurbozz


0

a simple way to navigate one page to another is startActivity new Intent(firstpagename.this,secondpagename.class);

2018-05-16 06:39
by ilcc chalakudy
Ads