我已经按照这个链接成功地在Android中制作了单例类. http://www.devahead.com/blog/2011/06/extending-the-android-application-class-and-dealing-with-singleton/
问题是我想要一个对象.就像我有活动A和活动B.在活动AI中从Singleton访问对象class.我使用该对象并对其进行了一些更改.
当我移动到Activity B并从Singleton Class访问该对象时,它给了我初始化的对象,并且不保留我在Activity A中所做的更改.还有其他方法可以保存更改吗?请高手帮帮我.这是MainActivity
public class MainActivity extends Activity {
protected MyApplication app;
private OnClickListener btn2=new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent=new Intent(MainActivity.this,NextActivity.class);
startActivity(intent);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Get the application instance
app = (MyApplication)getApplication();
// Call a custom application method
app.customAppMethod();
// Call a custom method in MySingleton
Singleton.getInstance().customSingletonMethod();
Singleton.getInstance();
// Read the value of a variable in MySingleton
String singletonVar = Singleton.customVar; …Run Code Online (Sandbox Code Playgroud)