I'm aware of three ways that by simply clicking a particular button, it moves from the current activity to another.
I'd like to know which of these three ways is the best one to take into consideration, the fluidity of the application. And if you can, let me know a case where each of these three forms is preferable.
Form 1:
It's the way I go there in the activity xml and in the button's scope I add an onClick to it and name a method. In the case: android:onClick="addNext"
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="genesysgeneration.twocases.MainActivity">
<Button
android:text="Next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/btnNext"
android:onClick="addNext"/>
</RelativeLayout>
This is done in MainActivity.java and I only add the method I have previously specified: addNext .
MainActivity.java:
package genesysgeneration.twocases;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
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 addNext(View v){
Intent it = new Intent(MainActivity.this, Main2Activity.class);
startActivity(it);
}
}
Form 2:
This is the way I use it most, because apart from being the first one I saw, it is the way the code gets better organized when the activity has several buttons. In it I have to implement the View.OnClickListener in the "public class" like this:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
But I notice that with this form I have to also declare the activity button, which does not happen in the way I showed previously. I named the btnNext button:
MainActivity.java:
package genesysgeneration.twocases;
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 AppCompatActivity implements View.OnClickListener{
private Button btnNext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnNext=(Button)findViewById(R.id.btnNext);
btnNext.setOnClickListener(this);
}
public void onClick(View v){
Intent it = new Intent(this, Main2Activity.class);
startActivity(it);
}
}
Form 3:
In it I also have to declare the button, but the difference is that the Intent is done within a method that already comes within the protected void onCreate and talz. In this way I also do not need to use implements , but if the activity has many buttons, protected void gets very polluted:
package genesysgeneration.twocases;
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 AppCompatActivity {
private Button btnNext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnNext=(Button)findViewById(R.id.btnNext);
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent it = new Intent(MainActivity.this, Main2Activity.class);
startActivity(it);
}
});
}
}
These three forms that I have demonstrated work perfectly, now I would like to know, as I have already said, the difference between them.