我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。
单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?
本案的任何替代方案
当前回答
源类:
Intent myIntent = new Intent(this, NewActivity.class);
myIntent.putExtra("firstName", "Your First Name Here");
myIntent.putExtra("lastName", "Your Last Name Here");
startActivity(myIntent)
目标类(NewActivity类):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
Intent intent = getIntent();
String fName = intent.getStringExtra("firstName");
String lName = intent.getStringExtra("lastName");
}
其他回答
您还可以通过创建可分割类来传递自定义类对象。使其可分割的最佳方法是编写类,然后简单地将其粘贴到如下站点http://www.parcelabler.com/.单击构建,您将获得新代码。复制所有这些并替换原始的类内容。然后-
Intent intent = new Intent(getBaseContext(), NextActivity.class);
Foo foo = new Foo();
intent.putExtra("foo", foo);
startActivity(intent);
并在NextActivity中获得结果-
Foo foo = getIntent().getExtras().getParcelable("foo");
现在,您可以像使用一样简单地使用foo对象。
换句话说,您可以使用接口传递数据。
我们有两个活动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 myIntent = new Intent(this, NewActivity.class);
myIntent.putExtra("firstName", "Your First Name Here");
myIntent.putExtra("lastName", "Your Last Name Here");
startActivity(myIntent)
目标类(NewActivity类):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
Intent intent = getIntent();
String fName = intent.getStringExtra("firstName");
String lName = intent.getStringExtra("lastName");
}
我们可以通过两种方式将值传递给另一个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
要访问所有活动中的会话id,必须将会话id存储在SharedPreference中。
请参见下面我用于管理会话的类,您也可以使用该类。
import android.content.Context;
import android.content.SharedPreferences;
public class SessionManager {
public static String KEY_SESSIONID = "session_id";
public static String PREF_NAME = "AppPref";
SharedPreferences sharedPreference;
SharedPreferences.Editor editor;
Context mContext;
public SessionManager(Context context) {
this.mContext = context;
sharedPreference = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
editor = sharedPreference.edit();
}
public String getSessionId() {
return sharedPreference.getString(KEY_SESSIONID, "");
}
public void setSessionID(String id) {
editor.putString(KEY_SESSIONID, id);
editor.commit();
editor.apply();
}
}
//Now you can access your session using below methods in every activities.
SessionManager sm = new SessionManager(MainActivity.this);
sm.getSessionId();
//below line us used to set session id on after success response on login page.
sm.setSessionID("test");