小智 5
另一种可能更容易的方法是为您的应用程序创建一个Application类.这保证只创建一次,并且在应用程序的生命周期内存在.除此之外,它还可以为您的应用程序提供"数据中心"功能,以便不同的活动可以轻松共享数据.所以,对于你的光标,你只需要像这样使用Application类的成员变量(警告,我从我的应用程序中复制了这段代码并在此处进行了编辑,因此无法保证编译.只需要显示这个想法.):
package com.jcascio.k03;
import android.app.Application;
import android.database.Cursor;
// use your application's name instead of "K03Application"
public class K03Application extends Application {
public final String TAG = "K03";
Cursor sharedCursor; // this cursor can be shared between different Activities
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onTerminate() {
super.onTerminate();
}
public Cursor getSharedCursor()
{
return this.sharedCursor;
}
public void setSharedCursor(Cursor c)
{
this.sharedCursor = c;
}
}
Run Code Online (Sandbox Code Playgroud)
可以从任何Activity中获取应用程序对象
this.getApplication()
// You cast it to your Application sub-class and call the Cursor accessor function
Cursor c = ((K03Application)this.getApplication()).getSharedCursor();
Run Code Online (Sandbox Code Playgroud)
因此,您的第一个Activity将从数据库中获取一些goo,它将作为Cursor返回给它.此活动将在应用程序中调用setSharedCursor.然后它将启动第二个Activity,它将在其onCreate函数(或任何其他函数)中调用getSharedCursor来检索游标.
| 归档时间: |
|
| 查看次数: |
4542 次 |
| 最近记录: |