4
Guys I have knowledge of three forms that through a simple click on a given button, it passes from the current Activity to another.
I would like to know which of these three ways is the best taking into account, 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 xml of Activity and the scope of button add an onClick to it and name a method. In this 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>
Once that’s done, I’ll go to Mainactivity.java and just add the method I specified earlier: 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 that I most use, because in addition to being the first one I saw, it is the way in which the code is best organized when Activity has several buttons. In it I have to implement the View.Onclicklistener in the "public class" in this way:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
But I notice that with this form I have to declare also the button of Activity, which does not happen in the way I showed earlier. I named the button btnNext:
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 the Implements, but if Activity has many buttons, protected void is 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 form that I have demonstrated work perfectly, now I would like to know, as I have said, the difference between them.
These are not 3 ways to "change Activity". They are ways to implement the onClick. Related (duplicate?) Advantage and advantage between onClick and setOnClickListener
– ramaral