在第一个下载数据并将其发送到主要活动的活动
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);
然后在主要活动中拾取并传输到碎片
//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);
在碎片
中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);
}
}
用户类
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;
}
}
我的问题是如何将数据传输到下一个活动。例如AddActivity。 运行上一个活动Login,Main和fragment
后运行AddActivity答案 0 :(得分:0)
我的问题是如何将数据传输到下一个活动
再次传递额外内容。
//in MainActivity
Intent myIntent = new Intent(LoginActivity.this, AddActivity.class);
myIntent.putExtras(getIntent().getExtras());
不要将用户的登录凭据传递给应用中的每一个,而是考虑使用PreferenceManager存储它们,或者按照此处的建议更安全:What is the most appropriate way to store user settings in Android application
答案 1 :(得分:0)
1)按意图:
来自活动登录的:
Intent i = new Intent (getApplicationContext(),MainActivity.class);
i.putExtra("iEmail","Xyz");
i.putExtra("iPassword","44562");
startActivity(i);
活动主页 中的
2)使用SQLite : 使用SQLite的优点: String Email= "";
String Password = "";
setContentView(R.layout.activity_main);
Bundle extras = getIntent().getExtras();
ID= extras.getString("iEmail");
Name= extras.getString("iPassword");
活动主页或任何活动/服务中的String iEmail = "someEmail@mail.com";
String iPassword = "some_password";
SQLiteDatabase db;
db = openOrCreateDatabase("DB_Name", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS Login(Email VARCHAR, Password VARCHAR);");
//drop table for clear all records and recreate
db.execSQL("DROP TABLE Login;");
db.execSQL("CREATE TABLE IF NOT EXISTS Login(Email VARCHAR, Password VARCHAR);");
db.execSQL("INSERT INTO Login VALUES('"+ iEmail +"','"+ iPassword +"',);");
String Email = "";
String Password = "";
SQLiteDatabase db;
db = openOrCreateDatabase("DB_Name", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS Login(Email VARCHAR, Password VARCHAR);");
Cursor c=db.rawQuery("SELECT * FROM Login", null);
if(c.getCount()==0) {
//do something if no records found
}
else{
while(c.moveToNext()){
Email = c.getString(0);
PAssword = c.getString(1);
}
}