What is the real difference between these three ways of switching from one activity to another?

4

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.

    
asked by anonymous 14.01.2017 / 20:07

2 answers

5
  

This is not 3 ways to "change Activity". They are rather forms of   implement onClick @Ramaral

Considering what Ramaral said, I will explain below ways to implement onClick , according to your doubt:

Form 2

View.OnClickListener {public static interface View.OnClickListener}

When using implements , in the strictness of the term, an interface in Java is nothing more than an abstract class composed only of abstract methods. And as such, it obviously can not be instantiated. That is, it only contains the declarations of the methods and constants, no implementation, only the 'template'.

And for what rays does a class serve without implementation? It is for other classes, based on this interface, to implement these methods for specific purposes.

It seems confusing and complicated, but it is as explained about interface: it will be a kind of communication between media. Usually between what is requested (of the functions it performs) and the implementation.

On Android, all View has the setOnClickListener method. This method takes as argument any object that has the onClick(View arg0) method. In Java, to make sure that a class has a certain method, there are interfaces. In this case, the interface used is the OnClickListener. See:

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 1 and Form 3

Specifying the android:onClick attribute results in instantiating Button by calling setOnClickListener internally. So there is absolutely no difference. These two snippets of code are all the same, but only implemented in two different ways.

Form 1

android: onClick {int onClick}

XML

<Button
    android:text="Next"
    android:id="@+id/btnNext"
    android:onClick="addNext"/>

Main

public void addNext() {
    Intent it = new Intent(this, Main2Activity.class);
    startActivity(it);
}

That's enough for the button to work. Every time the user clicks the button, they will be directed to another activity . It works? Yes, but ...

  • If you try to automatically rename (refactor) the onClick event method in Eclipse, it will not rename the method in the XML layout, breaking the code. If you do not fix it, the error would only give it at runtime.
  • In a very large application, it becomes unproductive to maintain these methods, as it is difficult to locate which views this method references, and vice versa.
  • If the method name is not explicitly explicit that it is a onClick event and is being referenced in an XML layout, or there is no comment specifying this, the programmer may not know this until an error occurs.

Form 3

setOnClickListener {method public class Button}

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);

        }
});

Conclusion

In performance, all of the above forms act in equal ways, having exactly the same behavior. But there are characteristic features between one and another that will depend on the design in which it will be used. See more about Advantage and disadvantage between onClick and setOnClickListener and also this article on Five different ways to use Event Listener (en) .

References

15.01.2017 / 04:36
2

Form 2 and 3 are the same thing. OnClick is a method of a callback interface.

Doing this from here:

new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(MainActivity.this, Main2Activity.class);
                startActivity(it);
            }
}

You are instantiating the ViewClass OnClickListener interface and you are implementing the onClick(View v) method;

And soon after, setting her up with a famous set method:

btnNext.setOnClickListener(View.onClicListener onClickListener);

Form 2 is not an interesting way when you have two Views that have different behaviors in a Click.

Form 1 is just another way that Google gives you to do the same thing of form 2. There is no difference in performance.

I'd rather use form two. Most of the literature you find around will use onClick like this.

  

The Intent is done within a method that already comes within the protected   void onCreate and talz.

This sentence is very wrong. The onClick method does not "already come in" from another method, as I've explained before, it's a method of an interface.

I strongly advise you to study the callback interface, also known as Listeners. It is widely used, not just in graphical interfaces. Using these interfaces, you greatly reduce the coupling of classes, especially when you make libraries that will be used by third parties.

    
14.01.2017 / 21:40