-1

So i have 2 activities that link to each other through an intent, but i need the second page to know what was clicked in the previous page. Heres what i mean.

public class featuredvendors extends AppCompatActivity {


public String nextpageref;

The string i would like to edit is declared above.

t2.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            nextpageref="post1";
            Intent t2 = new Intent(featuredvendors.this, featureddetails.class);

            startActivity(t2);
        }
    });

So essentially, i want to change the value of nextpageref based on what was clicked on. However it seems that i cant change the value inside an onClick block. Is there anyway i can pass the value "post1" to a string, but ONLY when this particular button is clicked? I need it this way because the next page 'featureddetails' needs to know this String value in order to pull the correct information from the DB.

  • Possible duplicate of [Pass a String from one Activity to another Activity in Android](http://stackoverflow.com/questions/6707900/pass-a-string-from-one-activity-to-another-activity-in-android) – Mohammed Atif Mar 29 '17 at 11:48
  • Class names is better to start with capital letters on each word: FeaturedVendors, FeaturedDetails etc – gvlachakis Mar 29 '17 at 11:50

2 Answers2

0

In the onClick block add this

t2.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            nextpageref="post1";
            Intent t2 = new Intent(featuredvendors.this, featureddetails.class);
t2.putString("key", nextpageref);
            startActivity(t2);
        }
    });

In the next activity get value from this piece of code in onCreate's method

Intent i= getIntent();
String value = i.getStringExtra("key");
Log.e("value", value);
Arsal Imam
  • 2,882
  • 2
  • 24
  • 35
0

SendingActivity

   Intent intent = new Intent(SendingActivity.this, RecievingActivity.class);
    intent. putString("keyName", value);  // pass your values and retrieve them in the other Activity using keyName
    startActivity(intent); 

Second Screen.java

public class newclass extends Activity { private TextView Textv;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.intent);
    Textv = (TextView)findViewById(R.id.tv2);
    Intent i= getIntent();
    i.getStringExtra("keyName");
}

}

omkar ugale
  • 106
  • 7