The idea that comes to my mind to write this post was to help some beginners in Android to let them know how to link two or more activities in Android. I am going to mention 3 ways in which you can do this.
This post is gonna be a very basic one but my main motive in it was that beginners sometimes don't know about this and they ask some dumb questions on StackOverflow. So here I am getting started with it. But first, you need to have at least two activities in your project. Suppose they are Activity1 and Activity2. The java files associated with them are Activity1.java and Activity2.java. Here I am assuming that you want to navigate from Activity1 to the Activity2
We will move from the easiest one to the difficult one and I'll explain why that method is used.
We will move from the easiest one to the difficult one and I'll explain why that method is used.
- The simple one So this method is the simplest one. You need to do is this:
- A bit complex one This one is the copy of the first one but split into declaration and usage like this:
- The difficult one So have you ever seen some apps open another one that is pre-installed? They use a method something like this. First, edit the Activity2 in the AndroidManifest.xml file like this and add a <intent-filter> like this:
startActivity(new Intent(Activity1.this, Activity2.class));This is used when you simply want to navigate to other activities
Intent openActivity = new Intent(Activity1.this, Activity2.class); startActivity(openActivity);Explanation: This method is used when we want to pass some custom parameters to the Intent. You will come to know more about flags attached to Intents in the later posts.
<activity android:name = "Activity2"> <intent-filter> <action android:name="com.test.Activity2" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
Intent openActivity = new Intent("com.test.Activity2"); //Change com.test with your own package name startActivity(openActivity);As I said this method is used to receive data from other apps, it can also be used to open your activity from other apps. But for this, the app must be installed on the device.
So these were the three methods you saw how to open another activity from one Activity. Hope you liked it. If yes, please let me know about it in the comments section below.
No comments:
Post a Comment
Please be polite to others while commenting. Spam content may be removed by the authors. Please be authentic in your reviews and opinion.