In the first activity of downloading data and sending them to the main activity
Intent myIntent = new Intent(LoginActivity.this, MainActivity.class);
Long userId = jsonResponse.getLong("id");
String username = jsonResponse.getString("username");
User user = new User(userId, username);
//from server to activity
myIntent.putExtra(MainActivity.USER_ID,userId);
myIntent.putExtra(MainActivity.USER, user);
startActivity(myIntent);
Then in the Main Activity picked up and transmitted to the Fragments
//class MainActivity
public static final String USER_ID = "user_id_key";
public static final String USER = "user_key";
private Long userId;
public User user,name;
//OnCreate
Intent intent = getIntent();
if(intent !=null) {
if(intent.hasExtra(USER_ID)) {
userId = intent.getLongExtra(USER_ID, 0L);
}
if(intent.hasExtra(USER)) {
user = (User) intent.getSerializableExtra(USER);
}
}
//setupViewPager
Bundle bundle = new Bundle();
bundle.putLong(USER_ID, userId);
bundle.putSerializable(USER, user);
In Fragments
private Long userId;
private User user;
//OnCreate
Bundle bundle= getArguments();
if(bundle !=null) {
if(bundle.containsKey(USER_ID)) {
userId = bundle.getLong(USER_ID, 0L);
}
if(bundle.containsKey(USER)) {
user = (User) bundle.getSerializable(USER);
}
}
User Class
package com.example.giftlist.giftlist.Data;
import java.io.Serializable;
public class User implements Serializable{
private Long id;
private String usename;
public User(Long id, String usename) {
this.id = id;
this.usename = usename;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsename() {
return usename;
}
public void setUsename(String usename) {
this.usename = usename;
}
}
My question is how to transfer the data to the next activity. AddActivity for example. AddActivity is run after you run the previous activity Login, Main and fragments