我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
当前回答
换句话说,您可以使用接口传递数据。
我们有两个活动A,B,那么我该怎么做,创建一个界面,如:
public interface M{
void data(String m);
}
然后,您可以调用赋值给这个方法,如下面A类中的代码所示:
public class A extends AppCompatActivity{
M m; //inteface name
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a);
m= (M) getActivity();
//now call method in interface and send data im sending direct you can use same on click
m.data("Rajeev");
}
}
现在您必须在类B中实现该接口:
public class B extends AppCompatActivity implements M{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.b);
}
@Override
public void data(String m) {
you can use m as your data to toast the value here it will be same value what you sent from class A
}
}
其他回答
科特林
从第一个活动传递
val intent = Intent(this, SecondActivity::class.java)
intent.putExtra("key", "value")
startActivity(intent)
参加第二项活动
val value = intent.getStringExtra("key")
建议
始终将密钥放在常量文件中,以便于管理。
companion object {
val KEY = "key"
}
考虑使用单例保存所有活动都可以访问的会话信息。
与额外变量和静态变量相比,此方法具有几个优点:
允许您扩展Info类,添加所需的新用户信息设置。您可以创建一个继承它的新类,或者只需编辑Info类,而无需更改所有地方的额外处理。易于使用-无需在每次活动中获得额外内容。公共类信息{私有静态Info实例;私有int id;private字符串名称;//私有构造函数不允许在create()或getInstance()方法之外创建实例私有信息(){}//用于从任何“活动”获取相同信息的方法。//它返回现有的Info实例,如果尚未创建,则返回null。公共静态信息getInstance(){返回实例;}//创建新的Info实例,或返回现有的实例(如果存在)。公共静态同步信息创建(int id,String name){if(空==实例){instance=new Info();instance.id=id;instance.name=名称;}返回实例;}}
我们可以通过两种方式将值传递给另一个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
来自活动
int n= 10;
Intent in = new Intent(From_Activity.this,To_Activity.class);
Bundle b1 = new Bundle();
b1.putInt("integerNumber",n);
in.putExtras(b1);
startActivity(in);
目标活动
Bundle b2 = getIntent().getExtras();
int m = 0;
if(b2 != null){
m = b2.getInt("integerNumber");
}
你只需要在表达你的意图时发送额外的信息。
这样地:
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);