0

I want a value to be passed from the first to the last activity when a button is clicked.

At the same time I want this button to bring me to the second activity. Somehow this doesn't work.

private void imagebutton1 () {
          ImageButton btn = (ImageButton) findViewById(R.id.imageButton4);
            btn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  //startActivity (new Intent(Akt1.this,Akt2.class));

                  String data1 = "data1";

                  Intent intent = new Intent (Akt1.this,Akt2.class);
                  Intent passdata_intent = new Intent(Akt1.this, Akt6.class);
                  passdata_intent.putExtra("data1",data1);

                 startActivity(passdata_intent);

              }
          });
HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
Dimi
  • 11
  • 5
  • Currently, you aren't doing anything with the "intent" Intent. What you are trying to achieve is quite unclear for me right now, could you explain more ? – ArteFact Jun 06 '17 at 08:28
  • when this button is pressed, i want the next activity to open and at the same time a value to be passed to my last activity " Akt6" – Dimi Jun 06 '17 at 08:49
  • Then I suggest you use SharedPreferences instead of an Extra, as said in some answers, you can't put an extra to an activity you're not going onto. You should save your value to prefs and to go to AKT2, then when you get to the AKT6, get it. **See my answer** – ArteFact Jun 06 '17 at 09:05
  • okay i tried it with sharedpreferences now but have a new problem. How can i do it that a value is only shown when the button is pressed? Right now it shows the same value whatever button i press – Dimi Jun 06 '17 at 09:47
  • your method only listens to the "ImageButton4" if you want each ImageButton to send a different value, you'll have to set a listener to each of them. – ArteFact Jun 06 '17 at 09:59

5 Answers5

0

i want a value to be passed from the first to the last activity when a button is clicked.

Sorry, it is impossible. You have to follow the activity stack.

To pass data from one to another, one intent had enough, no need to have two.

 Intent intent = new Intent (Akt1.this,Akt2.class);
 intent.putExtra("data1",data1);
 startActivity(intent);

Refer from how to directly pass value of a variable from 1st activty to 3rd activity using putextra?


Akt1

 Intent intent = new Intent (Akt1.this,Akt2.class);
 intent.putExtra("data1",data1);
 startActivity(intent);

Akt2

After setContentView

Intent intent = getIntent();
String data1 = intent.getStringExtra("data1");
John Joe
  • 12,412
  • 16
  • 70
  • 135
  • It has to be somehow possible that a value from the first activity is passed to the last one, so that i can make a calculation at the very end of the app :( maybe i used a wrong method? – Dimi Jun 06 '17 at 08:46
  • How your activity flow looked like? You can pass it from Activity one to two, then in activity two pass to three...until the last activity – John Joe Jun 06 '17 at 08:48
  • i thought about this too but how exactly do i do this? because i dont want the value to be shown in every activity. only in the last one. so how do i pass it through the others, without showing it? – Dimi Jun 06 '17 at 08:50
  • You can pass it without showing it. You can also save the data to database, in last activity, retrieve it. Easy – John Joe Jun 06 '17 at 08:51
  • how do i do that? – Dimi Jun 06 '17 at 08:53
  • Where you call the last activity ? In which activity ? – John Joe Jun 06 '17 at 08:53
  • i call the last activity " Akt6" in the second last " Akt5". But i pass a value to Akt6 from my first activity. – Dimi Jun 06 '17 at 08:55
  • then in Akt2 will it called Akt3 and so on ? – John Joe Jun 06 '17 at 08:59
  • basically in every Akt i have button that pass a value to Akt6 and open the next Akt. It is an app that makes a calculation in the end with the values – Dimi Jun 06 '17 at 09:04
0
private void imagebutton1 () {
        ImageButton btn = (ImageButton) findViewById(R.id.imageButton4);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {    
                String data1 = "data1";

                Intent intent = new Intent (Akt1.this,Akt2.class);
                intent.putExtra("data1",data1);

               startActivity(intent );

            }
        });

For example :

In MainActivity :

Intent intent = new Intent(this, OtherActivity.class);
intent.putExtra(OtherActivity.KEY_EXTRA, yourDataObject);
startActivity(intent);

In OtherActivity :

public static final String KEY_EXTRA = "com.example.yourapp.KEY_BOOK";

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  String yourDataObject = null;

  if (getIntent().hasExtra(KEY_EXTRA)) {
      yourDataObject = getIntent().getStringExtra(KEY_EXTRA);
  } else {
      throw new IllegalArgumentException("Activity cannot find  extras " + KEY_EXTRA);
  }
  // do stuff
}

More informations here : http://developer.android.com/reference/android/content/Intent.html

Dany Pop
  • 3,590
  • 2
  • 21
  • 28
0

i don't know why you need to start two activies with one button, but you can start first activity and in your first activity's on create you can start second activity (you can recreate your intent in first activity.)

Reza.Abedini
  • 2,227
  • 2
  • 16
  • 18
  • i have two buttons on my very first activity .. one button for "male" and one for "female" . Now regardless of what button is pressed, the next activity to be opened, is activity2. At the same time if e.g. the user pressed "male", then i want the variable " male" to be saved on my last activity of the app. – Dimi Jun 06 '17 at 08:45
0
   btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //startActivity (new Intent(Akt1.this,Akt2.class));

                String data1 = "data1";

                Intent intent = new Intent (Akt1.this,Akt2.class);
                intent.putExtra("data1",data1);
                startActivity(intent);
            }
        });

In your Akt2.class add this code

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);

    String data1 = getIntent().getExtras().getString("data1");

    // your code

}
Fakhriddin Abdullaev
  • 4,169
  • 2
  • 35
  • 37
0

Try the following :

            @Override
            public void onClick(View v) {
                //startActivity (new Intent(Akt1.this,Akt2.class));

                String data1 = "data1";

                getSharedPreferences("YOURKEY", MODE_PRIVATE).edit().putString("someKey", data1).apply();

                startActivity(new Intent (Akt1.this, Akt2.class));
            }

Then in AKT6 :

String data1 = getSharedPreferences("YOURKEY", MODE_PRIVATE).prefs.getString("someKey");
ArteFact
  • 517
  • 4
  • 14