我有一个场景,在通过登录页面登录后,每个活动都会有一个注销按钮。

单击注销时,我将传递要注销的登录用户的会话id。有人能指导我如何让所有活动都可以使用会话id吗?

本案的任何替代方案


当前回答

考虑使用单例保存所有活动都可以访问的会话信息。

与额外变量和静态变量相比,此方法具有几个优点:

允许您扩展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=名称;}返回实例;}}

其他回答

在当前活动中创建新的意向。

String myData="Your string/data here";
Intent intent = new Intent(this, SecondActivity.class);    
intent.putExtra("your_key",myData);
startActivity(intent);

在SecondActivity.java onCreate()中使用键your_key检索这些值

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        String myData = extras.getString("your_key");
    }  
}

要访问所有活动中的会话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");
/*
 * 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));
    }
}

我在类中使用静态字段,并获取/设置它们:

喜欢:

public class Info
{
    public static int ID      = 0;
    public static String NAME = "TEST";
}

要获取值,请在“活动”中使用:

Info.ID
Info.NAME

要设置值:

Info.ID = 5;
Info.NAME = "USER!";

您可以尝试共享首选项,这可能是在活动之间共享数据的一个很好的选择

保存会话id-

SharedPreferences pref = myContexy.getSharedPreferences("Session 
Data",MODE_PRIVATE);
SharedPreferences.Editor edit = pref.edit();
edit.putInt("Session ID", session_id);
edit.commit();

为了得到他们-

SharedPreferences pref = myContexy.getSharedPreferences("Session Data", MODE_PRIVATE);
session_id = pref.getInt("Session ID", 0);