For example i have activity1, activity2, activity3 and lastly valueAllActivity? how do I pass the data from activity1, activity2, activity3 to --> valueAllActivity?
I used a method in an activity to call another activity. It starts a game with two variables (resume and number of players).
private void MethodToCallAnotherActivity(int resume, int players) {
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
intent.putExtra(NextActivity.RESUME, resume);
intent.putExtra(NextActivity.PLAYERS, players);
startActivity(intent);
}
in the 'onCreate' of the NextActivity, I used
int resume = getIntent().getIntExtra(Game.RESUME, 1);
to read the variable I wanted.
Good luck!
You have several options:
Application
class, which you can then access in your activity by calling the getApplication()
method.Read the documentation for all available ways in which you can store and retrieve data in your application.
You would do it via intents. Assume data1 and data 2 are Strings and data3 is an int.
In your first activity when you set the intent to call the next activity:
Intent myIntent = new Intent(Activity1.this, Activity2.class);
myIntent.putExtra("Data1", data1);
myIntent.putExtra("Data2", data2);
myIntent.putExtra("Data3", data3);
Activity1.this.startActivity(myIntent);
Then in Activity 2:
Private String data1;
Private String data2;
Private int data3;
Bundle extras = getIntent().getExtras();
if (extras != null) {
data1 = extras.getString("Data1");
data2 = extras.getString("Data2");
data3 = extras.getInt("Data3");
}
// other code
Intent myIntent = new Intent(Activity2.this, Activity3.class);
myIntent.putExtra("Data1", data1);
myIntent.putExtra("Data2", data2);
myIntent.putExtra("Data3", data3);
Activity2.this.startActivity(myIntent);
And so on, through as many activities as you want.
You can Use any identifier you want. Above I used Data1, Data2, Data3. They could have as well been called Make, Model and TopSpeed. As long as you use the same id to get the data as you use to put it, it'll be fine.
EDIT
Several things I see...
First, use the getExtra
to get the data out of the bundle in your onCreate method for each activity. Put the intents to call the next activity wherever you need to.
Then, one of your issues is here in activity 2:
if (extras != null){
totalKalori1 = extras.getInt("totalBreakfast");
totalKalori2 = extras.getInt("totalLunch");
}
You haven't put totalLunch
into the bundle yet, so you shouldn't be trying to get
it yet. Delete that line.
Same thing in Activity 3:
if (extras != null){
totalKalori1 = extras.getInt("totalBreakfast");
totalKalori2 = extras.getInt("totalLunch");
totalKalori3 = extras.getInt("totalDinner");
}
You haven't put totalDinner
into the bundle yet, so you shouldn't be trying to get
it yet. Delete that line.
Then in Calculate All
you set an unnecessary intent and restart the activity... which looks to me like it would result in an infintie loop:
Intent n= new Intent(this, CalculateAll.class);
n.putExtra("totalBreakfast", totalKalori1);
n.putExtra("totalLunch", totalKalori2);
n.putExtra("totalDinner", totalKalori3);
startActivity(n);
Delete this whole section from the 'CalculateAll` activity.
I think moving the getExtra
parts and removing the bad data will fix your issue.
The recommended way to pass data from one activity to another is via intents.
Have a look at this tutorial.
You have some possibilities.
One that I like more is using the application context...
Create a new class like:
public class DataApp extends Application{
private int myInt;
private MyCustomObject myObj;
public int getMyInt() { return myInt; }
public void setMyInt(int i) { this.myInt = i; }
public MyCustomObject getMyObj() { return myObj; }
public void setMyObj(MyCustomObject ob) { this.myObj = ob;}
}
Add this to you manifest:
<application
android:name=".DataApp"
...>
...
</application>
After, when you need to pass data you can do this in your activity:
DataApp dataInfo = (DataApp)getApplicationContext();
//set some data:
dataInfo.setMyObj(/*put the object*/);
In your other activity, you get you object like this:
DataApp dataInfo = (DataApp)getApplicationContext();
MyCustomObject obj = dataInfo.getMyObj();
With this option, you can pass every object type you want.
To pass using the intent just do like that:
private String fUserName;
private String fPassword;
private Boolean fUseProxy;
private String fProxyAddress;
private Integer fProxyPort;
/** Called when the activity is first created. */
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard_activity);
fProjectsButton = (Button) findViewById(R.id.dashProjectsButton);
fUserName = "something";
fPassword = "xxx";
fUseProxy = false;
fProxyAddress = "";
fProxyPort = 80;
fProjectsButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(CodeBaseClientDashBoardActivity.this, CodeBaseClientProjectsActivity.class);
i.putExtra(CodeBaseClientSettingsActivity.PREFERENCE_API_USERNAME, fUserName);
i.putExtra(CodeBaseClientSettingsActivity.PREFERENCE_PASSWORD, fPassword);
i.putExtra(CodeBaseClientSettingsActivity.PREFERENCE_USE_PROXY, fUseProxy);
i.putExtra(CodeBaseClientSettingsActivity.PREFERENCE_PROXY_ADDRESS, fProxyAddress);
i.putExtra(CodeBaseClientSettingsActivity.PREFERENCE_PROXY_PORT, fProxyPort);
startActivity(i);
}
});
}