How to Go from one activity to another and another activity to the first activity
In this post, I am going to show you how to move the one activity to another on the click button.
In this post, we will use onclick method without used ID.
the simple method to go from one activity to another and another activity to the first activity easily.
When the user clicks on the button, the user redirects another Activity and another Activity click on Link then the user redirect first Activity.
Here Given step by step with source code
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:gravity="center" tools:context=".MainActivity"> <Button android:id="@+id/btnsubmit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="submit" android:text="submit" /> </LinearLayout>
package com.example.one2another; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate ( savedInstanceState ); setContentView ( R.layout.activity_main ); } public void submit(View view) { startActivity ( new Intent ( MainActivity.this,Main2Activity.class ) ); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:gravity="center" tools:context=".Main2Activity"> <TextView android:id="@+id/firstactivity" android:onClick="firstactivity" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Second Activity" android:textSize="30sp"> </TextView> </LinearLayout>
package com.example.one2another; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; public class Main2Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate ( savedInstanceState ); setContentView ( R.layout.activity_main2 ); } public void firstactivity(View view) { startActivity ( new Intent (Main2Activity.this,MainActivity.class ) ); } }
Download Source code