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

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

其他回答

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

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

我最近发布了Vapor API,这是一个jQuery风格的Android框架,它使类似这样的所有任务都更加简单。如前所述,SharedPreferences是实现此目的的一种方法。

VaporSharedPreferences实现为Singleton,因此这是一个选项,在VaporAPI中,它有一个重载的.put(…)方法,因此您不必明确担心您提交的数据类型-前提是它受支持。它也很流畅,因此您可以拨打电话:

$.prefs(...).put("val1", 123).put("val2", "Hello World!").put("something", 3.34);

它还可以选择自动保存更改,并在后台统一读写过程,因此您不需要像在标准Android中那样显式检索编辑器。

或者,您可以使用意向。在VaporAPI中,您还可以对VaporContent使用可链接的overload.put(…)方法:

$.Intent().put("data", "myData").put("more", 568)...

如其他答案中所述,将其作为附加项传递。您可以从“活动”中检索额外内容,此外,如果您正在使用VaporActivity,这将自动为您完成,因此您可以使用:

this.extras()

要在切换到的“活动”的另一端检索它们。

希望有人对此感兴趣:)

要访问所有活动中的会话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");
 Intent intent = new Intent(getBaseContext(), SomeActivity.class);
 intent.putExtra("USER_ID", UserId);
 startActivity(intent);

 On SomeActivity : 

 String userId= getIntent().getStringExtra("("USER_ID");

换句话说,您可以使用接口传递数据。

我们有两个活动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
    }
}