Almost every Andoid application has many activities, a screen that interact with user. On desktop based operating system, this screen is usually called by a “form”. It is very common that it is required to switch between one activity onto another activity in user interaction. In this post I will try to explain how switching between activities and passing variable into it can be done.
First we will have two activities, Activity1 and Activity2. In this post, we will try to switch from Activity1 to Activity2 and passing a String type variable from Activity1 to Activity2. An activity is a class which extends android.app.Activity class. Assuming Activity1 activity class has the following code:
public class Activity1 extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } } // end of class Activity1
Note that Activity1 inflate the main.xml as it’s layout and it’s consists only one button. Here’s the main.xml layout XML code:
<?xml version="1.0" encoding="utf-8"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/thebutton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/confirm" /> </LinearLayout>
Why with the button? We will use the button as the trigger in switching to Activity2 on it’s onClick event. So, inside the onCreate method add the following listener which listen to the button onClick event:
View.OnClickListener clickListener = new View.OnClickListener() { public void onClick(View v) { Context viewContext = v.getContext(); Intent i = new Intent(viewContext, Activity2.class); String stringToSend = "Some string"; String variableName = "id"; i.putExtra(variableName, stringToSend); startActivity(i); } };
This listener (clickListener) is a type of Android.view.View. There is an implemented onClick method which we’ll be going to used it to start the Activity2 activity and passing a variable into Activity2. The step is quite simple. First we need to create a new “intent” of Activity2 from button’s view context with the following code:
Context viewContext = v.getContext(); Intent i = new Intent(viewContext, Activity2.class);
Then we put our passing variable as an extra to the intent:
String stringToSend = "Some string"; String variableName = "id"; i.putExtra(variableName, stringToSend);
Lastly, start the activity with the intent.
startActivity(i);
That’s it. But we haven’t finished yet. we haven’t attach the listener to the button onClick
event yet so that we able to switch to Activity2 by the button onClick event. To attach the listener, we need to get the button instance and set it’s onClickListener to the clickListener that we have previously created. Add the following code inside the activity’s onCreate method to attach the listener to the button onClick event:
Button b = (Button)findViewById(R.id.thebutton); b.setOnClickListener(clickListener);
OK, we’re done on the Activity1 side. With the above code, we will have successfully switched to Activity2 after we click on the button.
Getting the passed variable after switching
In order to get the passing variable use the following code in Activity2 activity:
public class Activity2 extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity2); Bundle extras = getIntent().getExtras(); CharSequence text = extras.getCharSequence("id"); // do something with the text here... } }
The above activity uses activity2.xml layout. You need to define those layout on your resources definition.
Here’s how it’s work. We had previously passed the variable “id” into the intent. So in order to get the passed variable in Activity2, we need to get the intent first. Getting the intent is as simply as:
getIntent();
Getting the “extras” variable can be done by:
Bundle extras = getIntent().getExtras();
Because of the variable we had passed earlier has a String or CharSequence type, then we are able to get our variable by:
CharSequence text = extras.getCharSequence("id");
That’s it. You have successfully switch from one activity into another activity by using a simple button click and get the variables passed into it. Hope this tutorial is useful to you. If you found any error or if have any suggestions, please don’t hesitate to inform or simply comment this post. Thank you for reading!
This post can also be seen on:
http://aryo.info/blog/switching-between-activites-in-android-sdk.html