Question

How to navigate from one Activity screen to another Activity screen? In the first screen I'm having one button if I click the button it has to move to another Activity screen.

Was it helpful?

Solution

OnClickListener onClickListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        startActivity(new Intent(action));
    }
};

Button button = (Button) findViewById(id);
button.setOnClickListener(onClickListener);

OTHER TIPS

The most trivial case (called from activity):

startActivity(new Intent(this, ActivityToLaunch.class));

More details here: http://developer.android.com/guide/topics/fundamentals.html

Button x.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) 
                {
                    Intent i = new Intent(y.this, Activity.class);
                    startActivity(i);                   
                }
        });

Here we've defined a listener for Button x. The OS will call this method and start the Activity referenced in Intent i.

Here's the official tutorial example: http://developer.android.com/guide/tutorials/notepad/notepad-ex2.html

Button btn = (Button)findViewById(R.id.button1);

btn.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {      

        startActivity(new Intent(TestActivity.this,second.class));

    }
});
public void onClick(View v) 
{
    Intent myintent = new Intent(currentclass.this, nextactivity.class);
    startActivity(myintent);                               
}
final Context cont = this;
Button btnClickABC =(Button)findViewById(R.id.btnClickABC);
btnClickABC.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {                   
                startActivity(new Intent(cont, NextActivity.class));

            }
        });

This task can be accomplished using one of the android's main building block named as Intents and One of the methods public void startActivity (Intent intent) which belongs to your Activity class.

An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.

An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.

Refer the official docs -- http://developer.android.com/reference/android/content/Intent.html

public void startActivity (Intent intent) -- Used to launch a new activity.

So suppose you have two Activity class and on a button click's OnClickListener() you wanna move from one Activity to another then --

  1. PresentActivity -- This is your current activity from which you want to go the second activity.

  2. NextActivity -- This is your next Activity on which you want to move.

So the Intent would be like this

Intent(PresentActivity.this, NextActivity.class)

Finally this will be the complete code

  public class PresentActivity extends Activity {
        protected void onCreate(Bundle icicle) {
            super.onCreate(icicle);

            setContentView(R.layout.content_layout_id);

            final Button button = (Button) findViewById(R.id.button_id);
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    // Perform action on click   

                    Intent activityChangeIntent = new Intent(PresentActivity.this, NextActivity.class);

                    // currentContext.startActivity(activityChangeIntent);

                    PresentActivity.this.startActivity(activityChangeIntent);
                }
            });
        }
    }

This exmple is related to button click you can use the code anywhere which is written inside button click's OnClickListener() at any place where you want to switch between your activities.

Use following code..I hope this will help you.

 Button button = (Button)findViewById(R.id.xxx);
    button .setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
        Intent intent=new Intent(CurrentActivity.this,NextActivity.class);
        startActivity(intent);
        }
    });

xxx is id from your xml of your Button.

startActivity(new Intent(this,newActivity.class));

Switching from one activity to another is really simple, but tricky for a new one. Your next class must be defined in AndroidManifest.xml. This is tester class:

<activity
  android:name=".Tester"
  android:label="@string/title_activity_tester" >`enter code here`
</activity>


final Button button = (Button) findViewById(R.id.btnGo);// btnGo is id 
button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        Intent i = new Intent(CurrentClass.this, Tester.class);
        startActivity(i);
    }

You can navigate to the next screen using these code snippets:

Kotlin

startActivity(Intent(this, LoginActivity::class.java))

Java

startActivity(new Intent(this, LoginActivity.class))

Here's a reference: Android Developers - Starting another activity

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top