我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
当前回答
科特林
从第一个活动传递
val intent = Intent(this, SecondActivity::class.java)
intent.putExtra("key", "value")
startActivity(intent)
参加第二项活动
val value = intent.getStringExtra("key")
建议
始终将密钥放在常量文件中,以便于管理。
companion object {
val KEY = "key"
}
其他回答
你只需要在表达你的意图时发送额外的信息。
这样地:
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);
/*
* If you are from transferring data from one class that doesn't
* extend Activity, then you need to do something like this.
*/
public class abc {
Context context;
public abc(Context context) {
this.context = context;
}
public void something() {
context.startactivity(new Intent(context, anyone.class).putextra("key", value));
}
}
换句话说,您可以使用接口传递数据。
我们有两个活动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
}
}
如果要在活动/片段之间传输位图
活动
在活动之间传递位图
Intent intent = new Intent(this, Activity.class);
intent.putExtra("bitmap", bitmap);
在“活动”类中
Bitmap bitmap = getIntent().getParcelableExtra("bitmap");
碎片
在片段之间传递位图
SecondFragment fragment = new SecondFragment();
Bundle bundle = new Bundle();
bundle.putParcelable("bitmap", bitmap);
fragment.setArguments(bundle);
在SecondFragment内部接收
Bitmap bitmap = getArguments().getParcelable("bitmap");
传输大型位图
如果您正在获取失败的绑定器事务,这意味着您正在通过将大型元素从一个活动转移到另一个活动来超出绑定器事务缓冲区。
因此,在这种情况下,您必须将位图压缩为字节数组,然后在另一个活动中解压缩,如下所示
在第一个活动中
Intent intent = new Intent(this, SecondActivity.class);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPG, 100, stream);
byte[] bytes = stream.toByteArray();
intent.putExtra("bitmapbytes",bytes);
在第二次活动中
byte[] bytes = getIntent().getByteArrayExtra("bitmapbytes");
Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
在活动之间传递数据最方便的方法是传递意图。在要发送数据的第一个活动中,应添加代码,
String str = "My Data"; //Data you want to send
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("name",str); //Here you will add the data into intent to pass bw activites
v.getContext().startActivity(intent);
您还应导入
import android.content.Intent;
然后在下一个活动(SecondActivity)中,您应该使用以下代码从意图中检索数据。
String name = this.getIntent().getStringExtra("name");