Reputation: 836
I would love some help (newish to android).
I currently have 4 activities.The first three activities has EditText fields in them, and then the final activity displays all the information that has been entered up to now. Seems simple enough,right?
So after looking on this site I discovered the way to do this was to use putExtra() and getExtra();
This doesn't quite seem to work as all the things I add to the extras hashset are not available at the final activity. Any advice?
Thanks
code below for help
package com.example.smallchangebigloss;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class EnterName extends Activity {
private EditText edit;
private Bundle extras = getIntent().getExtras();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enter_name);
edit = (EditText) findViewById(R.id.nameEdit);
}
public Bundle getExtras(){
return extras;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.enter_name, menu);
return true;
}
public void next(View view) {
if (edit.getText().toString().equals("")) {
new AlertDialog.Builder(this).setTitle("Ut Oh!")
.setMessage("Please enter your name.")
.setNeutralButton("Try again", null).show();
}
else {
Intent i = new Intent(getApplicationContext(), CurrentWeight.class);
i.putExtra("name", edit.getText().toString());
startActivity(i);
}
}
}
package com.example.smallchangebigloss;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class CurrentWeight extends Activity {
private EditText edit;
private String name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_current_weight);
Bundle extras = getIntent().getExtras();
name = extras.getString("name");
edit = (EditText) findViewById(R.id.currentWeightEdit);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.current_weight, menu);
return true;
}
public void next(View view) {
if (edit.getText().toString().equals("")) {
new AlertDialog.Builder(this).setTitle("Ut Oh!")
.setMessage("Please enter your current weight.")
.setNeutralButton("Try again", null).show();
}
else {
Intent i = new Intent(getApplicationContext(),GoalWeight.class);
i.putExtra("name", name);
i.putExtra("current", edit.getText().toString());
startActivity(i);
}
}
}
package com.example.smallchangebigloss;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class GoalWeight extends Activity {
private EditText edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_goal_weight);
Bundle extras = getIntent().getExtras();
System.out.println(extras.containsKey("name"));
extras.containsKey("current");
edit = (EditText) findViewById(R.id.currentWeightEdit);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.goal_weight, menu);
return true;
}
public void next(View view) {
if (edit.getText().toString().equals("")) {
new AlertDialog.Builder(this).setTitle("Ut Oh!")
.setMessage("Please enter your Goal Weight.")
.setNeutralButton("Try again", null).show();
}
else {
Intent i = new Intent(getApplicationContext(), Complete.class);
i.putExtra("goal", edit.getText().toString());
startActivity(i);
}
}
}
package com.example.smallchangebigloss;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class Complete extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
// TextView current = (TextView) findViewById(R.id.completeCurrentMod);
// current.setText("hi");
//// current.setText(extras.getString("current")); These lines break it
setContentView(R.layout.activity_complete);
System.out.println("name: " + extras.getString("name") + "Current weight: "
+ extras.getString("current") + "goal: " + extras.getString("goal"));//Only displaying last ones...bundle them everytime in each class?
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.complete, menu);
return true;
}
}
Upvotes: 0
Views: 910
Reputation: 2549
You are creating a new Intent in each of your text entry Activities and starting your Complete Activity. The first time your Complete Activity instance is created you will get the Intent extra and depending upon which Activity fired, the intent from that Activity.
If your Complete Activity is destroyed and recreated it will have the intent only from the one that created it.
Now if your Complete Activity is already created and running and you are firing intents from other Activity you should look at
http://developer.android.com/reference/android/app/Activity.html#onNewIntent(android.content.Intent)
@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
// get your new intent data here
}
As an aside, in a real world app you would not be your entry fields strewn across Activities, so I am assuming you are just trying things out, which is cool.
Upvotes: 1
Reputation: 2340
In the first Activity, get the strings into a variable from the EditText
.And
Intent in = new Intent(getApplicationContext(), B.class);
in.putExtra("TAG", String);
startActivity(in);
In the second activity, get it by
Intent x = getIntent();
String variable = x.getStringExtra("TAG");
And so on..
Upvotes: 0
Reputation: 1346
The problem you are facing is you need to pass your values from one class to the other in each class. So in GoalWeight
Activity you need to pass "name" and "current" values also.
So do the following when starting the next Activity.
Intent i = new Intent(getApplicationContext(), Complete.class);
i.putExtra("goal", edit.getText().toString());
i.putExtra("name", extras.getString("name"));
i.putExtra("current", extras.getString("current"));
startActivity(i);
Upvotes: 1
Reputation: 44571
It looks like you aren't attaching all of the extras
to each new Intent
. Since you create a new Intent
in each Activity
, as you need to be doing, you also need to attach all of the extras
from the previous Activities
. The easiest way is to create a Bundle then get this Bundle
in each activity and add the new extras
to the Bundle
each time.
Another way you could do it so you don't have to keep track of extras
or a Bundle
in each is to use SharedPreferences. Just add the value to a new preference each time then get them all in the final Activity
. That link has a good example of creating and getting the SharedPreferences
Upvotes: 2
Reputation: 46856
Here is roughly what your code is doing:
Activity 1:
Start with no extras
Create and save extra called "name"
Activity 2:
Read name out of extras
create and save extra called "current"
save the value of name
Activity 3:
Read the whole bundle of extras
Create and save extra called "goal"
//Do nothing with the whole bundle
Activity 4:
Read the whole bundle of extras
Inside of Activity 3 you are pulling the values out of extras but not "re-saving" them so that they get passed along to the final activity. So when you get to activity4 the only extra that you should have is "goal"
because that is the only one that you added at the end of Activity3.
Upvotes: 1