putExtra and getExtra






 
XML



<Button android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:layout_marginTop="15dp" 
   android:text="Go Next Page" 
   android:onClick="goNextPage"
   android:id="@+id/button" 
   app:layout_constraintHorizontal_bias="0.202" 
   app:layout_constraintLeft_toLeftOf="parent"
   app:layout_constraintRight_toRightOf="parent" 
   app:layout_constraintTop_toBottomOf="@+id/tv1"
   tools:layout_constraintLeft_creator="1" 
   tools:layout_constraintRight_creator="1" 
   tools:layout_constraintTop_creator="1" /> 








JAVA 1st Page



public class MainActivity extends AppCompatActivity {
    public final static String EXTRA_MESSAGE = "Message From First Page:";
     String itemSelected ="coke";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
    }//ONcREATE

    public void goNextPage(View view)
    { 
        Intent intent = new Intent(this, NextActivity.class);         
        intent.putExtra("item", itemSelected);  //Passing the values coke to next Page
        startActivity(intent);
    }
}

JAVA 2nd Page

public class NextActivity extends AppCompatActivity {
    private TextView messageReceived;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page2);


        Intent intent = getIntent();
        String itemSelected = intent.getStringExtra("item"); //Receiving the values from previous Page

        Toast.makeText(getApplicationContext(), itemSelected , Toast.LENGTH_SHORT).show();


    }

}