How to create Second Activity bypassing a value with Android Studio

 

btn.setTag(data) – We bypass our value into a memory of Viewholder pattern and get the value from our OnClickListener of View.

Intent i = new Intent(MainActivity.this,Main2Activity.class);

Every app could have many Activities. To invoke other Activity we use Intent class to send a value to a BroadCastReceiver.

i.putExtra(“T”,data);

Bypass our variable to another Intent.

 

 

We create a MainActivity using the functions above.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
String data = "Tomato";
Button btn = findViewById(R.id.button);
btn.setTag(data);


btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        createActivity((String)v.getTag());
    }
});

    }
public void createActivity(String data){

        Intent i = new Intent(MainActivity.this,Main2Activity.class);
        i.putExtra("T",data);

        startActivity(i);
    }
}

Bundle extras = getIntent().getExtras();
To retrieve data from Intent you have to use Bundle class.

The next step is to create a second activity “Main2Activity”

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        TextView tx = findViewById(R.id.textView);
        int sum = 0;
        String newString;


        try {
            if (savedInstanceState == null) {
                Bundle extras = getIntent().getExtras();
                if (extras == null) {
                    newString = null;
                } else {
                    newString = extras.getString("T");
                }
            } else {
                newString = (String) savedInstanceState.getSerializable("T");
            }
            tx.setText(newString);
        }
        catch(NullPointerException ex){
            ex.printStackTrace();
        }

    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="146dp"
        android:layout_marginTop="276dp"
        android:layout_marginEnd="177dp"
        android:layout_marginBottom="407dp"
        android:text="CLICK"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context=".Main2Activity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="142dp"
        android:layout_height="57dp"
        android:layout_marginStart="132dp"
        android:layout_marginTop="232dp"
        android:layout_marginEnd="137dp"
        android:layout_marginBottom="442dp"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>

Leave a Reply