我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
当前回答
使用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");
}
其他回答
第一种方式:在当前“活动”中,当您创建一个意图打开新屏幕的对象时:
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类使用序列化在类之间传递数据。
正如埃里希指出的那样,传递意图附加是一个好方法。
然而,Application对象是另一种方式,当跨多个活动处理同一状态时(而不是必须在任何地方获取/放置它),或者处理比原语和字符串更复杂的对象时,它有时更容易。
您可以扩展Application,然后使用getApplication()从任何活动(在同一应用程序中)设置/获取所需的内容。
还要记住,您可能看到的其他方法,如静态,可能会有问题,因为它们可能会导致内存泄漏。应用程序也有助于解决这一问题。
活动之间的数据传递主要通过意向对象进行。
首先,必须使用Bundle类将数据附加到intent对象。然后使用startActivity()或startActivityForResult()方法调用活动。
您可以从博客文章“将数据传递给活动”中找到更多信息。
它帮助我从上下文中看事情。这里有两个例子。
转发数据
主要活动
将要发送的数据与键值对放在Intent中。有关密钥的命名约定,请参见此答案。使用startActivity启动第二个活动。
主要活动.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// "Go to Second Activity" button click
public void onButtonClick(View view) {
// get the text to pass
EditText editText = (EditText) findViewById(R.id.editText);
String textToPass = editText.getText().toString();
// start the SecondActivity
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra(Intent.EXTRA_TEXT, textToPass);
startActivity(intent);
}
}
第二项活动
使用getIntent()获取启动第二个活动的Intent。然后,可以使用getExtras()和在第一个活动中定义的键提取数据。由于我们的数据是一个字符串,所以我们在这里只使用getStringExtra。
第二活动.java
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
// get the text from MainActivity
Intent intent = getIntent();
String text = intent.getStringExtra(Intent.EXTRA_TEXT);
// use the text in a TextView
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(text);
}
}
传回数据
主要活动
使用startActivityForResult启动第二个活动,为其提供任意结果代码。覆盖ActivityResult。当“第二个活动”完成时调用此函数。通过检查结果代码,可以确保它实际上是第二个活动。(当您从同一主活动启动多个不同的活动时,这很有用。)提取您从返回意向中获得的数据。使用键值对提取数据。我可以使用任何字符串作为密钥,但我将使用预定义的Intent.EXTRA_TEXT,因为我正在发送文本。
主要活动.java
public class MainActivity extends AppCompatActivity {
private static final int SECOND_ACTIVITY_REQUEST_CODE = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// "Go to Second Activity" button click
public void onButtonClick(View view) {
// Start the SecondActivity
Intent intent = new Intent(this, SecondActivity.class);
startActivityForResult(intent, SECOND_ACTIVITY_REQUEST_CODE);
}
// This method is called when the second activity finishes
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// check that it is the SecondActivity with an OK result
if (requestCode == SECOND_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// get String data from Intent
String returnString = data.getStringExtra(Intent.EXTRA_TEXT);
// set text view with string
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(returnString);
}
}
}
}
第二项活动
将要发送回上一个活动的数据放入意向。数据使用键值对存储在Intent中。我选择使用Intent.EXTRA_TEXT作为密钥。将结果设置为result_OK并添加保存数据的意图。调用finish()关闭第二个活动。
第二活动.java
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
// "Send text back" button click
public void onButtonClick(View view) {
// get the text from the EditText
EditText editText = (EditText) findViewById(R.id.editText);
String stringToPassBack = editText.getText().toString();
// put the String to pass back into an Intent and close this activity
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_TEXT, stringToPassBack);
setResult(RESULT_OK, intent);
finish();
}
}
通过捆绑对象从此活动传递参数启动另一个活动
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);