2011-10-25 105 views
9

我有一个需要使用多个活动的登录会话ID。我如何在多个活动之间分享这些常见数据?目前,我将这些数据传递给Intent,但无法正常工作。对于某些活动,我传递一些其他数据,并且常见数据丢失。如何在多个活动之间共享相同的数据

+1

独居

Session mSession = Session.getInstance(); mSession.setSessionId("your_session_id"); 

读?并在此发布您的代码 –

+0

您可以通过意向共享数据,也可以通过该目的创建1个全局类和访问数据。通过意图发送数据非常简单..只需将您的数据放入intent.putExtra(合适的数据类型)的方法,并分享了多个活动... – android

+0

我已经在我的应用程序登录,我会得到sessionid我需要跨登录后所有活动使用它它也需要传递一些其他数据到下一个活动(包括sessionid)。现在我尝试了意图,但它有时会转移到其他活动。 – Harish

回答

1

一种方式做到这一点是延长Application,然后调用你的活动getApplicationThis site有一些例子。

+0

不好的解决方案,但它写得很久以前..所以我推动作者改进它:) – Ewoks

10

使用共享偏好是这样的:

SharedPreferences myprefs= this.getSharedPreferences("user", MODE_WORLD_READABLE); 
myprefs.edit().putString("session_id", value).commit(); 

您可以检索在您的应用程序这个信息是这样的:

SharedPreferences myprefs= getSharedPreferences("user", MODE_WORLD_READABLE); 
String session_id= myprefs.getString("session_id", null); 

当你想从当前活动开始另一个活动时,你应该使用意图......如果孩子活动正在完成,也应该使用意图Y依赖于从父活动数据...使用意图

1

我不知道究竟是什么代码是你正在处理,但是你试过就像在你当前活动这个

,创建一个意图

Intent i = new Intent(getApplicationContext(), ActivityB.class); 
i.putExtra(key, value); 
startActivity(i); 

然后在其他活动中检索这些值。

Bundle extras = getIntent().getExtras(); 
if(extras !=null) { 
    String value = extras.getString(key); 
} 
5

使用Singleton类进行共享。

示例代码

public class Category { 

    private static final Category INSTANCE = new Category(); 
    public String categoryName = ""; 
    public int categoryColor = 0; 
    public boolean status = false; 
    // Private constructor prevents instantiation from other classes 
    private Category() {} 

    public static Category getInstance() { 
     return INSTANCE; 
    } 
} 
在其他活动/类

用于设定值:

Category cat; 
cat=Category.getInstance(); 

cat.categoryName="some name"; 
cat.status=ture; 

for getting the values every where you want in your application. 
Category cat; 
cat=Category.getInstance(); 

String sq=cat.categoryName; 
boolean stat=cat.status; 
0

,我做的是,我延伸从活动全局类,然后一路延伸用户将从中进行交互的实际活动。这就是我学会如何通过一些“学习编写Android代码”的书来完成的。

这和使用Application类非常相似。

+0

内存泄漏怎么办?你如何分享数据? – Nico

2

在阅读关于SharedPreferences的文档和关于Singleton vs Application in android的近乎火焰之战的讨论之后。

我的结论是:请反驳任何结论

  • SharedPreferences:好,如果你打算只保留原语和易于保持不同的包裹。
  • Parcelable:一个低级别的解决方案,有很多样板代码,你可以用在Extras
  • 可串行化:如果你不想打扰Parcelable。见this
  • 单身人士:我的选择。简单,快速,没有样板。我的贡献是在里面使用一个地图灵活性。

    public class Config { 
    
        private static Config instance; 
        private HashMap<String, Object> map; 
    
        /*Keep doc about keys and its purpose if needed*/ 
    
        public static final String TOKEN = "token"; 
        public static final String SESSION = "sessionId"; 
    
        /** A bean with A LOT of useful user info */ 
        public static final String USER_BEAN = "userBean"; 
    
        private Config() { 
         map = new HashMap<String, Object>(); 
        } 
    
        public static final Config getInstance() { 
         if (instance == null) { 
          instance = new Config(); 
         } 
         return instance; 
        } 
    
        public void setKey(String key, Object value) { 
         map.put(key, value); 
        } 
    
        public Object getKey(String key) { 
         return map.get(key); 
        } 
    } 
    
  • 应用:几乎相同的单,但对于一些隐藏到我的理由,减轻测试和做事更多的Android标化的方式。

0

您可以将其存储在SharedPreferences或Singleton类中。 我会建议使用单例类。

  • SharedPreferences:

写值

SharedPreferences mPref= this.getSharedPreferences("session", MODE_WORLD_READABLE); 
mPref.edit().putString("session_id", your_session_value).commit(); 

读取值

SharedPreferences mPref= getSharedPreferences("session", MODE_WORLD_READABLE); 
String sSession_id= mPref.getString("session_id", null); 
  • 单例类

    public class Session { 
    
        private static Session session = null; 
    
        private String mSessionId; 
    
        private Session() { 
        } 
    
        public static Session getInstance() { 
         if (session == null) { 
          session = new Session(); 
         } 
         return session; 
        } 
    
        public String getSessionId() { 
         return mSessionId; 
        } 
    
        public void setSessionId(String pSessionId) { 
         this.mSessionId = pSessionId; 
        } 
    } 
    

写从单

Session mSession = Session.getInstance(); 
String mSessionId = mSession.getSessionId(); 
要准确地传递什么
相关问题