我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
当前回答
我们可以通过两种方式将值传递给另一个Activity(已经发布了相同类型的答案,但我在这里发布了通过意图尝试的redcing代码)
1.通过意图
Activity1:
startActivity(new Intent(getApplicationContext(),Activity2.class).putExtra("title","values"));
InActivity 2:
String recString= getIntent().getStringExtra("title");
2.通过SharedPreference
Activity1:
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
// 0 - for private mode
Editor editor = pref.edit();
editor.putString("key_name", "string value"); // Storing string
editor.commit(); // commit changes
Activty2:
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
pref.getString("key_name", null); // getting String
其他回答
我在类中使用静态字段,并获取/设置它们:
喜欢:
public class Info
{
public static int ID = 0;
public static String NAME = "TEST";
}
要获取值,请在“活动”中使用:
Info.ID
Info.NAME
要设置值:
Info.ID = 5;
Info.NAME = "USER!";
使用Intent将数据传递到一个Activity to AnotheActivity的最佳方法,
检查剪下的代码
活动One.java
Intent myIntent = new Intent(this, NewActivity.class);
myIntent.putExtra("key_name_one", "Your Data value here");
myIntent.putExtra("key_name_two", "Your data value here");
startActivity(myIntent)
第二次活动
第二活动.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
Intent intent = getIntent();
String valueOne = intent.getStringExtra("key_name_one");
String valueTwo = intent.getStringExtra("key_name_two");
}
您可以使用SharedPreferences。。。
登录中。SharedPreferences中的时间存储会话idSharedPreferences preferences=getSharedPreferences(“会话”,getApplicationContext().MODE_PRIVATE);编辑器编辑器=preferences.edit();editer.putString(“sessionId”,sessionId);editer.commit();注销。共享引用中的时间提取会话idSharedPreferences preferences=getSharedPreferences(“会话”,getApplicationContext().MODE_PRIVATE);String sessionId=首选项.getString(“sessionId”,null);
如果您没有所需的会话id,请删除sharedpreferences:
SharedPreferences settings = context.getSharedPreferences("session", Context.MODE_PRIVATE);
settings.edit().clear().commit();
这非常有用,因为一次保存值,然后检索任何活动。
要访问所有活动中的会话id,必须将会话id存储在SharedPreference中。
请参见下面我用于管理会话的类,您也可以使用该类。
import android.content.Context;
import android.content.SharedPreferences;
public class SessionManager {
public static String KEY_SESSIONID = "session_id";
public static String PREF_NAME = "AppPref";
SharedPreferences sharedPreference;
SharedPreferences.Editor editor;
Context mContext;
public SessionManager(Context context) {
this.mContext = context;
sharedPreference = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
editor = sharedPreference.edit();
}
public String getSessionId() {
return sharedPreference.getString(KEY_SESSIONID, "");
}
public void setSessionID(String id) {
editor.putString(KEY_SESSIONID, id);
editor.commit();
editor.apply();
}
}
//Now you can access your session using below methods in every activities.
SessionManager sm = new SessionManager(MainActivity.this);
sm.getSessionId();
//below line us used to set session id on after success response on login page.
sm.setSessionID("test");
你只需要在表达你的意图时发送额外的信息。
这样地:
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("Variable name", "Value you want to pass");
startActivity(intent);
现在,在SecondActivity的OnCreate方法中,您可以像这样获取额外内容。
如果您发送的值过长:
long value = getIntent().getLongExtra("Variable name which you sent as an extra", defaultValue(you can give it anything));
如果发送的值是字符串:
String value = getIntent().getStringExtra("Variable name which you sent as an extra");
如果发送的值是布尔值:
Boolean value = getIntent().getBooleanExtra("Variable name which you sent as an extra", defaultValue);