我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话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 = intent.getStringExtra("SESSION_ID");
Log.d("Session_id : " + sessionId);
}
else{
Toast.makeText(SignOutActivity.this, "Intent is null", Toast.LENGTH_SHORT).show();
}
}
其他回答
//您的问题是,您希望在登录后存储会话id,并将该会话id用于要注销的每个活动。
//问题的解决方案是成功登录后必须将会话id存储在公共变量中。每当您需要注销会话id时,您可以访问该变量并将变量值替换为零。
//Serializable class
public class YourClass implements Serializable {
public long session_id = 0;
}
我们可以通过两种方式将值传递给另一个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
标准方法。
Intent i = new Intent(this, ActivityTwo.class);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
String getrec=textView.getText().toString();
Bundle bundle = new Bundle();
bundle.putString(“stuff”, getrec);
i.putExtras(bundle);
startActivity(i);
现在,在第二个活动中,从捆绑包中检索数据:
获取捆绑包
Bundle bundle = getIntent().getExtras();
提取数据…
String stuff = bundle.getString(“stuff”);
第一种方式:在当前“活动”中,当您创建一个意图打开新屏幕的对象时:
String value="xyz";
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
intent.putExtra("key", value);
startActivity(intent);
然后在onCreate方法的nextActivity中,检索从上一个活动传递的值:
if (getIntent().getExtras() != null) {
String value = getIntent().getStringExtra("key");
//The key argument must always match that used send and retrieve value from one activity to another.
}
第二种方法:您可以创建一个bundle对象并将值放入bundle中,然后将bundle对象放入当前活动的意图中-
String value="xyz";
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("key", value);
intent.putExtra("bundle_key", bundle);
startActivity(intent);
然后在onCreate方法的nextActivity中,检索从上一个活动传递的值:
if (getIntent().getExtras() != null) {
Bundle bundle = getIntent().getStringExtra("bundle_key");
String value = bundle.getString("key");
//The key argument must always match that used send and retrieve value from one activity to another.
}
您还可以使用bean类使用序列化在类之间传递数据。
您可以使用intent类在Activities之间发送数据。它基本上是一条发送给操作系统的消息,您可以在其中描述数据流的源和目的地。类似于从A到B活动的数据。
在活动A(源)中:
Intent intent = new Intent(A.this, B.class);
intent.putExtra("KEY","VALUE");
startActivity(intent);
在活动B(目标)->
Intent intent =getIntent();
String data =intent.getString("KEY");
在这里,您将获得密钥“key”的数据
为了更好地使用,为了简单起见,密钥应该存储在类中,这将有助于将键入错误的风险降至最低
这样地:
public class Constants{
public static String KEY="KEY"
}
现在在活动A中:
intent.putExtra(Constants.KEY,"VALUE");
在活动B中:
String data =intent.getString(Constants.KEY);