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

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

本案的任何替代方案


当前回答

我最近发布了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()

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

希望有人对此感兴趣:)

其他回答

我们可以通过两种方式将值传递给另一个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

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

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

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

正如埃里希指出的那样,传递意图附加是一个好方法。

然而,Application对象是另一种方式,当跨多个活动处理同一状态时(而不是必须在任何地方获取/放置它),或者处理比原语和字符串更复杂的对象时,它有时更容易。

您可以扩展Application,然后使用getApplication()从任何活动(在同一应用程序中)设置/获取所需的内容。

还要记住,您可能看到的其他方法,如静态,可能会有问题,因为它们可能会导致内存泄漏。应用程序也有助于解决这一问题。

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

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

第一种方式:在当前“活动”中,当您创建一个意图打开新屏幕的对象时:

String value="xyz";
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);    
intent.putExtra("key", value);
startActivity(intent);

然后在onCreate方法的nextActivity中,检索从上一个活动传递的值:

if (getIntent().getExtras() != null) {
      String value = getIntent().getStringExtra("key");
      //The key argument must always match that used send and retrieve value from one activity to another.
  }

第二种方法:您可以创建一个bundle对象并将值放入bundle中,然后将bundle对象放入当前活动的意图中-

String value="xyz";
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);  
Bundle bundle = new Bundle();
bundle.putInt("key", value);  
intent.putExtra("bundle_key", bundle);
startActivity(intent);

然后在onCreate方法的nextActivity中,检索从上一个活动传递的值:

if (getIntent().getExtras() != null) {
      Bundle bundle = getIntent().getStringExtra("bundle_key");    
      String value = bundle.getString("key");
      //The key argument must always match that used send and retrieve value from one activity to another.
  }

您还可以使用bean类使用序列化在类之间传递数据。