如果我正确理解,你需要一种方法来存储的变量数据或一个数据结构或任何内容,不要失去这个数据时,整个severals活动你的应用程序的变化,对不对?您需要在多个活动中保存状态。
解决方案一:在Java中,对于这个问题的一个解决方案是使用“静态”变量。你可以做,但使用Android,我们可以使用更优雅的解决方案。
第二和建议的解决方案:通过应用程序上下文(容易)
您应该创建自己的android.app.Application的子类相关联的状态。它将像单身人士一样工作。应用
一个子类继承应用程序的性能,你可以,无论你想使用命令“Context.getApplicationContext()”访问此类。通常你会使用这个类去完成需要全局访问的东西。例如:
class YourName_App extends Application {
private ArrayList<String> chatConversation;
public String getChatConversation(){
return this.chatConversation;
}
public void setChatConversation(ArrayList<String> chat){
this.chatConversation = chat;
}
}
现在你的聊天活动:
class Chat extends Activity {
ArrayList<String> conversation;
@Override
public void onCreate(Bundle b)
{
...
YourName_App appState = ((YourName_App) getApplicationContext());
conversation = appState.getChatConversation();
...
}
}
成了!这是做到这一点的最佳方式。
对不起,我英文很差。
不要忘记在'AndroidManifest.xml'中声明你的应用程序子类。即。 ' ' –
howettl
2012-05-07 20:09:22