Home » Android » Moving from One activity to next in Android

Moving from One activity to next in Android

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am developing an application in android which has a login Screen. Right now I am able to receive the response from the server successfully. After a successful response it should take me to the next activity or class where I display a new screen/activity. what should I do in order to achieve this.

How to&Answers:

In Android you are using Intents to change from one Activity to another. In this case you would use an explicit Intent. In code this would like this:

Intent goToNextActivity = new Intent(getApplicationContext(), YourNewClass.class);
startActivity(goToNextActivity);

Be sure to add YourNewClass to the manifest as another activity like this:

<activity android:name=".your.package.YourNewClass" />

Have a closer look at the documentation of Intent. You can also read the document about application fundamentals in the documentation it is somewhat to deep to just answer this question but it will give you insights in the most important concepts of android.

Answer:

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.

Answer:

public void onClick(View arg0) 
{
       Intent intent = new Intent(context, App2Activity.class);
       startActivity(intent);
}