我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
当前回答
使用回调在活动之间进行新的实时交互:
-步骤01:实现共享接口
public interface SharedCallback {
public String getSharedText(/*you can define arguments here*/);
}
-步骤02:实现共享类
final class SharedMethode {
private static WeakReference<Context> mContext;
private static SharedMethode sharedMethode = new SharedMethode();
private SharedMethode() {
super();
}
public static SharedMethode getInstance() {
return sharedMethode;
}
public void setContext(Context context) {
if (mContext != null)
return;
mContext = new WeakReference<Context>(context);
}
public boolean contextAssigned() {
return mContext != null && mContext.get() != null;
}
public Context getContext() {
return mContext.get();
}
public void freeContext() {
if (mContext != null) mContext.clear();
mContext = null;
}
}
-步骤03::在第一个活动中玩代码
public class FirstActivity extends Activity implements SharedCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
// call playMe from here or there
playMe();
}
private void playMe() {
SharedMethode.getInstance().setContext(this);
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
@Override
public String getSharedText(/*passed arguments*/) {
return "your result";
}
}
-步骤04::在SecondActivity中完成游戏
public class SecondActivity extends Activity {
private SharedCallback sharedCallback;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
if (SharedMethode.getInstance().contextAssigned()) {
if (SharedMethode.getInstance().getContext() instanceof SharedCallback)
sharedCallback = (SharedCallback) SharedMethode.getInstance().getContext();
// to prevent memory leak
SharedMethode.freeContext();
}
// You can now call your implemented methodes from anywhere at any time
if (sharedCallback != null)
Log.d("TAG", "Callback result = " + sharedCallback.getSharedText());
}
@Override
protected void onDestroy() {
sharedCallback = null;
super.onDestroy();
}
}
步骤05::您还可以实现backword回调(从First到Second),以从SecondAvctivity获取一些结果或调用一些方法
其他回答
这是我的最佳实践,当项目规模巨大且复杂时,它会帮助很大。
假设我有两个活动,LoginActivity和HomeActivity。我想将2个参数(用户名和密码)从LoginActivity传递到HomeActivity。
首先,我创建HomeIntent
public class HomeIntent extends Intent {
private static final String ACTION_LOGIN = "action_login";
private static final String ACTION_LOGOUT = "action_logout";
private static final String ARG_USERNAME = "arg_username";
private static final String ARG_PASSWORD = "arg_password";
public HomeIntent(Context ctx, boolean isLogIn) {
this(ctx);
//set action type
setAction(isLogIn ? ACTION_LOGIN : ACTION_LOGOUT);
}
public HomeIntent(Context ctx) {
super(ctx, HomeActivity.class);
}
//This will be needed for receiving data
public HomeIntent(Intent intent) {
super(intent);
}
public void setData(String userName, String password) {
putExtra(ARG_USERNAME, userName);
putExtra(ARG_PASSWORD, password);
}
public String getUsername() {
return getStringExtra(ARG_USERNAME);
}
public String getPassword() {
return getStringExtra(ARG_PASSWORD);
}
//To separate the params is for which action, we should create action
public boolean isActionLogIn() {
return getAction().equals(ACTION_LOGIN);
}
public boolean isActionLogOut() {
return getAction().equals(ACTION_LOGOUT);
}
}
以下是我如何在LoginActivity中传递数据
public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
String username = "phearum";
String password = "pwd1133";
final boolean isActionLogin = true;
//Passing data to HomeActivity
final HomeIntent homeIntent = new HomeIntent(this, isActionLogin);
homeIntent.setData(username, password);
startActivity(homeIntent);
}
}
最后一步,这里是我如何在HomeActivity中接收数据
public class HomeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//This is how we receive the data from LoginActivity
//Make sure you pass getIntent() to the HomeIntent constructor
final HomeIntent homeIntent = new HomeIntent(getIntent());
Log.d("HomeActivity", "Is action login? " + homeIntent.isActionLogIn());
Log.d("HomeActivity", "username: " + homeIntent.getUsername());
Log.d("HomeActivity", "password: " + homeIntent.getPassword());
}
}
完成!酷:)我只是想分享我的经验。如果你在做小项目,这应该不是大问题。但当你在大项目上工作时,当你想进行重构或修复bug时,这真的很痛苦。
活动之间的数据传递主要通过意向对象进行。
首先,必须使用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();
}
}
/*
* 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));
}
}
数据对象应扩展Parcelable或Serializable类。
Intent mIntent = new Intent(FirstActivity.this, SecondActivity.class);
mIntent.putExtra("data", data);
startActivity(mIntent);