What is the real difference between these three ways of changing from one Activity to another?

Asked

Viewed 775 times

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.

2 answers

5


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

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

Form 2

View.Onclicklistener { public Static interface View.Onclicklistener }

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

And what the hell good is a class without implementation? It serves for other classes, based on this interface, to implement these methods for specific purposes.

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

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

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

Specify the attribute android:onClick results in instantiating the Button calling for setOnClickListener internally. Therefore, there is absolutely no difference. These two code snippets are totally equal, 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);
}

This is enough for the button to work. Every time the user clicks the button, it will be directed to another Activity. Does it work? Yes, but...

  • If you try to rename (refactor) the event method onClick automatically, in Eclipse, it will not rename the method in the XML layout, breaking the code. If you do not correct, the error would only give at runtime.
  • In a very large application, it becomes unproductive to maintain these methods, as it becomes difficult to locate which ones views that this method refers to and vice versa.
  • If the method name is not very explicit it is an event onClick and is being referenced in a layout XML, 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);

        }
});

Completion

In terms of performance, all the above-mentioned forms act in equal ways, possessing exactly the same behavior. But there are characteristic facilities between one and the other that will depend on the project in which it will be used. See more about Advantage and advantage between onClick and setOnClickListener and also this article on Five different ways to use the Event Listener.

References

2

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

Doing it 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 Onclicklistener interface of the View Class and implementing the method onClick(View v);

And right 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 one Click.

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

I prefer to use the two form. Most of the literature you find around will use onClick like this.

Intent is done within a method that already comes within protected void onCreate and Talz.

That phrase is very wrong. The onClick method does not "already come in" from another method, as I explained before, it is a method of an interface.

I strongly advise you to study callback interface, also known as Listeners. It is widely used, not only in graphical interfaces. Making use of these interfaces, you greatly reduce class coupling, especially when making libraries that will be used by third parties.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.