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

单击注销时,我将传递要注销的登录用户的会话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");
}

其他回答

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

喜欢:

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

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

Info.ID
Info.NAME

要设置值:

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

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

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

允许您扩展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 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");

使用回调在活动之间进行新的实时交互:

-步骤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获取一些结果或调用一些方法

在CurrentActivity.java中编写以下代码

Intent i = new Intent(CurrentActivity.this, SignOutActivity.class);
i.putExtra("SESSION_ID",sessionId);
startActivity(i);

按以下方式访问SignOutActivity.java中的SessionId

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_out);
    Intent intent = getIntent();
    
    // check intent is null or not
    if(intent != null){
        String sessionId = intent.getStringExtra("SESSION_ID");
        Log.d("Session_id : " + sessionId);
    }
    else{
        Toast.makeText(SignOutActivity.this, "Intent is null", Toast.LENGTH_SHORT).show();
    }
}