如何在Android应用程序中的活动之间传递数据?
我有一个场景,通过登录页面登录后,每个activity
都会有一个注销button
。
在点击登出时,我会传递登录用户的session id
来注销。 任何人都可以指导我如何保持session id
可用于所有activities
?
任何替代这种情况
//你的问题是你想在登录后存储会话ID,并为每个要注销的活动提供会话ID。
你的问题的解决方案是你必须在成功登录公共变量后存储你的会话ID。 并且每当你需要注销会话ID时,都可以访问该变量并将变量值替换为零。
//Serializable class public class YourClass implements Serializable { public long session_id = 0; }
这是将会话ID传递给所有活动的最简单方法之一。
Intent mIntent = new Intent(getApplicationContext(), LogoutActivity.class); mIntent.putExtra("session_id", session_id); startActivity(mIntent);
所以从LogoutActivity你可以获取session_id ,这将进一步用于注销操作。
希望这会有帮助…谢谢
在CurrentActivity.java中写下面的代码
Intent i = new Intent(CurrentActivity.this, SignOutActivity.class); i.putExtra("SESSION_ID",sessionId); startActivity(i);
在SignOutActivity.java中访问SessionId的方式如下
public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_sign_out); Intent intent = getIntent(); // check intent is null or not if(intent != null){ String sessionId = i.getStringExtra("SESSION_ID"); Log.d("Session_id : " + sessionId); } else{ Toast.makeText(SignOutActivity.this, "Intent is null", Toast.LENGTH_SHORT).show(); } }
对于在所有活动中使用会话ID,您可以按照以下步骤操作。
1 – 在应用程序的应用程序文件中定义一个STATIC VARIABLE会话(它将保存会话ID的值)。
2 – 现在使用类引用调用会话变量,在此处获取会话ID值并将其分配给静态变量。
3 – 现在你可以在任何地方通过调用静态变量来使用这个会话ID值
通过Bundle对象从这个活动传递参数开始另一个活动
Intent intent = new Intent(getBaseContext(), YourActivity.class); intent.putExtra("USER_NAME", "xyz@gmail.com"); startActivity(intent);
检索另一个活动(YourActivity)
String s = getIntent().getStringExtra("USER_NAME");
这对于简单类型的数据类型是可以的。 但是如果你想在活动之间传递复杂的数据,你需要首先序列化它。
这里我们有员工模型
class Employee{ private String empId; private int age; print Double salary; getters... setters... }
你可以使用google提供的Gson lib来像这样序列化复杂的数据
String strEmp = new Gson().toJson(emp); Intent intent = new Intent(getBaseContext(), YourActivity.class); intent.putExtra("EMP", strEmp); startActivity(intent); Bundle bundle = getIntent().getExtras(); String empStr = bundle.getString("EMP"); Gson gson = new Gson(); Type type = new TypeToken<Employee>() { }.getType(); Employee selectedEmp = gson.fromJson(empStr, type);
考虑使用单例来保存所有活动可访问的会话信息。
与extras和static变量相比,这种方法有几个优点:
- 允许您扩展信息类,添加您需要的新用户信息设置。 您可以创建一个新的类继承它,或者只需编辑Info类,而无需在所有位置更改额外处理。
-
使用简单 – 无需在每个活动中获得额外的费用。
公开课信息{
private static Info instance; private int id; private String name; //Private constructor is to disallow instances creation outside create() or getInstance() methods private Info() { } //Method you use to get the same information from any Activity. //It returns the existing Info instance, or null if not created yet. public static Info getInstance() { return instance; } //Creates a new Info instance or returns the existing one if it exists. public static synchronized Info create(int id, String name) { if (null == instance) { instance = new Info(); instance.id = id; instance.name = name; } return instance; }
}
如果你使用kotlin:
在MainActivity1中:
var intent=Intent(this,MainActivity2::class.java) intent.putExtra("EXTRA_SESSION_ID",sessionId) startActivity(intent)
在MainActivity2中:
if (intent.hasExtra("EXTRA_SESSION_ID")){ var name:String=intent.extras.getString("sessionId") }
在“活动”之间共享数据的一个好方法是使用静态字段和方法创建一个类。
在我的例子中,我想在活动之间共享ArrayLists <String>。
public class Values { private static Map<String, ArrayList<String>> xValues; public static init(){ if(xValues == null) xValues = new HashMap<String, ArrayList<String>>(); } public static ArrayList<String> getStringList(String key){ return xValues.get(key); } public static void putStringList(ArrayList<String> value, String key){ xValues.put(key, value); } }
然后在Main类中只有一次初始化。 那么你只需在其他类中使用它的静态方法:
主要
(不要创建任何对象,因为您将使用静态方法和字段。):
Values.init();
在任何活动或甚至任何类放置一个新的ArrayList
ArrayList<String> list = new ArrayList<String>(); list.add("Text"); Values.putStringList(list, "mylist");
在任何活动,甚至任何类别获得
for(String s:Values.getStringList("mylist")) Log.d("My list",s);
将会产生一个日志条目:我的列表:文本。
结论:一个好的方法是使用Java的可能性:静态方法和字段可以在我们包中的所有活动中访问。