Hello all fellow programmers. Today I have another topic about the fun and exciting android development world. We’ll it’s sort of a beginner kind of topic, but I want to help you guys become experts in Android development so I think it can be justified. 🙂 Anyways, when I started programming for android I was having trouble with concept of an activity; it seems pretty straightforward, but I think that it can get confusing when you’re starting to develop your first Android application.
Android Activity
An android activity is a java class which is the back-end functionality to a user interface. It’s a very simple concept right? Well then another question came into my head, how do navigate through multiple activities. Well, my friends I will give a short example on how to navigate between activities in this post.
Example:
First Activity
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> <Button android:id="@+id/nextActivityButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Go to second activity" /> </TableLayout>
package nerdyrocks.com.android_intents_example; import android.app.Activity; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends Activity implements View.OnClickListener { // Variables: private Button nextActivityButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.nextActivityButton = (Button)this.findViewById(R.id.nextActivityButton); this.nextActivityButton.setOnClickListener(this); } @Override public void onClick(View view) { // Launching the sign up activity: Intent intent = new Intent(this, secondActivity.class); startActivity(intent); } }
Second Activity:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".secondActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="I am in the second activity" /> <Button android:id = "@+id/backButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Go back to first activity" /> </android.support.constraint.ConstraintLayout>
package nerdyrocks.com.android_intents_example; import android.app.Activity; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class secondActivity extends Activity implements OnClickListener { private Button backButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); this.backButton = (Button)this.findViewById(R.id.backButton); this.backButton.setOnClickListener(this); } @Override public void onClick(View view) { onBackPressed(); } }
So, as you can see in the first activity we have a button named nextActivityButton which is pressed in order to go to the next activity. We set a OnclickListener on the nextActivityButton, so once it is clicked then the code inside the onClick() function is fired, and that’s where all of the cool magic of changing between activities happens. We create an intent, in this case I named it intent. The Intent object takes two parameters. The first parameter is the instance of the current class and the second parameter is the name of the class in which it will go into next. In this case, the second activity is called secondActivity; oh ya don’t forget to put the .class part in the end. Whala, now we are in the second activity. Simple enough, right! and the activities should look something like this:
First Activity User Interface:

Second Activity User Interface:

Thanks for making it to the end. Have fun tweaking this example in order to use it for your own projects! Keep coding my friends.