Title: Open another Activity in Android using Explicit intent
link : Open another Activity in Android using Explicit intent
Open another Activity in Android using Explicit intent
In this android application we will learn how to open another activity of our project. We will use Explicit intent to open another activity first we'll lean what is intent than what is explicit intent:
Intent:- Intent is an android service that gives us the facilities of accessing one activity from the other activity. Basically, There are two types of Intent:
1) Implicit Intent
2) Explicit Intent
Explicit Intent:- Accessing one user define activity from the other activity like open new page of the application.
Here we are giving example of Implicit intent. So, create new project and drop one text view and one button. The code of android XML files is given below:
(Main XML file:- activity_main.xml)
Intent:- Intent is an android service that gives us the facilities of accessing one activity from the other activity. Basically, There are two types of Intent:
1) Implicit Intent
2) Explicit Intent
Explicit Intent:- Accessing one user define activity from the other activity like open new page of the application.
Here we are giving example of Implicit intent. So, create new project and drop one text view and one button. The code of android XML files is given below:
(Main XML file:- activity_main.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#373"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="50dp" android:text="first Activity" android:textSize="35sp" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:layout_marginTop="42dp" android:onClick="call" android:text="Click to call Second" /> </RelativeLayout>
(Second XML file:- second.xml)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#077"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Second Activity" android:layout_marginTop="20dp" android:layout_marginLeft="30dp" android:textSize="35sp" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="call" android:layout_marginLeft="110dp" android:text="Go to first" /> </LinearLayout>
Now open Java file and initialize all objects. Use the following trick to use explicit intent:
Intent obj=new Intent(From,To);
The code of both android Java files is given below with description:
(Main Java file:- MainActivity.java)
(Second Java file:- SecondActivity.java)
Now update AndroidManifist.xml file to use another activity in your project. Use the code below </activity> tag:
(Main Java file:- MainActivity.java)
package sel.expli; //you package name import android.os.Bundle; import android.view.View; import android.app.Activity; import android.content.Intent; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //set layout setContentView(R.layout.activity_main); } //This method is called when we click on button //and it is declare in main XML file in button tag //we have to pass View object in this method public void call(View v) { //define in intent where you want to go //use Intent(From,To) like we are sending a post Intent i=new Intent(this, SecondActivity.class); //start the given action to Intent startActivity(i); //finish this activity finish(); } }
(Second Java file:- SecondActivity.java)
package sel.expli; //you package name import android.os.Bundle; import android.view.View; import android.app.Activity; import android.content.Intent; public class SecondActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //set layout setContentView(R.layout.second); } //See the explanation of main activity //both are same public void call(View v) { Intent i=new Intent(this, MainActivity.class); startActivity(i); finish(); } }
Now update AndroidManifist.xml file to use another activity in your project. Use the code below </activity> tag:
<activity android:name="sel.expli.SecondActivity" android:label="@string/app_name"> </activity>
Here sel.expli is your package name and SecondActivity (SeconActivity.java) is your second activity name.
Intent-filter in Android XML file is used to define which activity will launch first. Now run your project and test application. If you have any doubt please comment.
Intent-filter in Android XML file is used to define which activity will launch first. Now run your project and test application. If you have any doubt please comment.
That's Open another Activity in Android using Explicit intent
That's an article Open another Activity in Android using Explicit intent
This time, hopefully can benefit for you all. Well, see you in other article postings.
You are now reading the article Open another Activity in Android using Explicit intent With link address https://u-see1.blogspot.com/2017/06/open-another-activity-in-android-using.html
Belum ada tanggapan untuk "Open another Activity in Android using Explicit intent"
Post a Comment