[菜鸟询问]单独建一个 Activity 提供 Context 合适吗? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
Duluku

[菜鸟询问]单独建一个 Activity 提供 Context 合适吗?

  •  1
     
  •   Duluku Dec 8, 2016 12531 views
    This topic created in 3427 days ago, the information mentioned may be changed or developed.

    想写一个 SQLite 的小项目,我想在任何线程地方直接对数据库进行修改,想做个静态对象。测试发现只有最开始创建 Helper 的时候用到了 Context ,所以有两种方法。

    1. 第一种,在程序最开始就在 Activity 中调用一次getWritableDatabase然后以后就可以应该就不用 Context 了
    2. 第二种,单独建一个 Activity 提供 Context ,这样也解决很多其他的问题,比如某线程内使用 Toast ,但是总觉得这种方式不太好。

    望各位 V 友指教 Orz

    ps. 其实就是对 Context 这种东西不是很熟,各位是怎么用的?

    private static MyDatabaseHelper dbHelper = null;//静态对象引用 public static MyDatabaseHelper getInstance(Context context) { if (dbHelper == null) { Log.i(TAG, "getInstance: 创建 helper"); // 发现只有最开始创建的时候使用了 Context ,以后也不会再用了。 dbHelper = new MyDatabaseHelper(context); } return dbHelper; } private MyDatabaseHelper(Context context) { super(context, DB_NAME, null, VERSION); } 
    Supplement 1    Dec 8, 2016
    顺便想问问另一个细节,我想在某个线程里 Toast , 这里的 Context 应该怎么获取呢? 要么是从之前的 Activity 或者 Service 传过来,还是有别的办法? :)
    20 replies    2016-12-08 19:02:54 +08:00
    viator42
        1
    viator42  
       Dec 8, 2016   1
    定义 Application 类不就行了
    Duluku
        2
    Duluku  
    OP
       Dec 8, 2016
    @viator42 不是很明白诶,能详细说一下吗? 谢谢
    1023400273
        3
    1023400273  
       Dec 8, 2016   1
    直接在 Application 内搞定
    Duluku
        4
    Duluku  
    OP
       Dec 8, 2016
    @1023400273 怎么在 Application 内搞定嘞。。不是特别明白。。。多谢回复 :)
    1023400273
        5
    1023400273  
       Dec 8, 2016
    数据库类做成一个单例,在 Application 内初始化
    shanjinwei
        7
    shanjinwei  
       Dec 8, 2016 via Android   1
    …为什么不写个 application 弄个单例
    Duluku
        8
    Duluku  
    OP
       Dec 8, 2016
    @q397064399 链接帮大忙了,多谢多谢
    Chrisplus
        9
    Chrisplus  
       Dec 8, 2016   1
    Context.getApplicationContext()
    额外贴一段 Android Developer Blog 里的话

    In summary, to avoid context-related memory leaks, remember the following:

    - Do not keep long-lived references to a context-activity (a reference to an activity should have the same life cycle as the activity itself)
    - Try using the context-application instead of a context-activity
    - Avoid non-static inner classes in an activity if you don't control their life cycle, use a static inner class and make a weak reference to the activity inside. The solution to this issue is to use a static inner class with a WeakReference to the outer class, as done in ViewRoot and its W inner class for instance
    - A garbage collector is not an insurance against memory leaks
    Duluku
        10
    Duluku  
    OP
       Dec 8, 2016
    @Chrisplus 我好像明白了,可以这么理解吗,如果这个 Sqlite 我传的 Context 是某个 Activity ,那么这个 Activity 要是被销毁,这个 dbHelper 会被销毁掉, 但是如果这个 Sqlite 我传的 getApplicationContext 的话,这个 dbHelper 是基于 Application 的,所以直到 App 被销毁才是被销毁。 所以这里只能使用 getApplicationContext 。
    Duluku
        11
    Duluku  
    OP
       Dec 8, 2016
    @Chrisplus 顺便想问问另一个细节,我想在某个线程里 Toast , 这里的 Context 应该怎么获取呢? 要么是从之前的 Activity 或者 Service 传过来,还是有别的办法? :)
    bqbkbz
        12
    bqbkbz  
       Dec 8, 2016
    @Duluku 反了,如果你拿的是 activitycontext ,因为你的静态类不会销毁导致 activity 也不会销毁,这就出现了内存泄漏,静态类是和 application 一样的生命周期,所以需要用 applicationcontext
    bqbkbz
        13
    bqbkbz  
       Dec 8, 2016   1
    @Duluku toast 也可以使用 applicationcontext 吧
    Chrisplus
        14
    Chrisplus  
       Dec 8, 2016
    @Duluku
    1. 参考楼上,你的理解反了。
    2. Toast 应该与 application context 绑定,参考 google 的 sample code

    https://developer.android.com/guide/topics/ui/notifiers/toasts.html#CustomToastView

    Context cOntext= getApplicationContext();
    CharSequence text = "Hello toast!";
    int duration = Toast.LENGTH_SHORT;

    Toast toast = Toast.makeText(context, text, duration);
    toast.show();
    xingda920813
        15
    xingda920813  
       Dec 8, 2016
    用 Realm 替代 SQLite.
    Duluku
        16
    Duluku  
    OP
       Dec 8, 2016
    @zhaohui318 是的,我明白了! 类似这么写就好了,所有的 Context 也解决了。以后还是要认真对待这个 Context ,不能乱传呀
    ```
    public class CustomApplication extends Application {

    private static MyDatabaseHelper myDatabaseHelper;
    private static Context context;

    public static Context getContext() {
    return context;
    }

    public static MyDatabaseHelper getMyDatabaseHelper() {
    return myDatabaseHelper;
    }

    @Override
    public void onCreate()
    {
    super.onCreate();
    myDatabaseHelper = new MyDatabaseHelper(getApplicationContext());
    cOntext= getApplicationContext();
    }

    }
    ```
    bazingaterry
        17
    bazingaterry  
       Dec 8, 2016
    singleton?
    Duluku
        18
    Duluku  
    OP
       Dec 8, 2016
    @Chrisplus @zhaohui318 多谢多谢,你的回复对我很有帮助
    JayFang1993
        19
    JayFang1993  
       Dec 8, 2016
    有些地方还必须要当前页面 Activity 的 context 比如弹对话框
    Duluku
        20
    Duluku  
    OP
       Dec 8, 2016
    @JayFang1993 没错没错,这个时候的 Context 必须要传下去的... :)
    About     Help     Advertise     Blog     API     FAQ     Solana     3595 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 82ms UTC 04:39 PVG 12:39 LAX 21:39 JFK 00:39
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86