简体   繁体   English

方向更改时处理SQLiteOpenHelper

[英]Handle SQLiteOpenHelper when orientation changes

The apps I try to create is a ViewPager then consist of Fragments . 我尝试创建的应用程序是ViewPager然后由Fragments组成。

Each Fragment has different data, because the got it from Database . 每个Fragment都有不同的数据,因为它来自Database

I send the Database class from Fragment Activity to Fragment inside the FragmentStatePagerAdapter . 我将Fragment Activity的Database类发送到Fragment内的FragmentStatePagerAdapter

Here is the code : 这是代码:

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
    public ScreenSlidePagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return PageFragment.create(position, student, getApplicationContext(), mDbStudent);
    }

    @Override
    public int getCount() {
        return NUM_PAGES;
    }

}

mDbStudent is the Database class. mDbStudent是Database类。

I open my mDbStudent at onCreate 我在onCreate打开我的mDbStudent

And I close my mDbStudent when onDestroy 然后我在onDestroy时关闭我的mDbStudent

The problem I have is when The orientation changes, the mDbStudent was destroyed but the fragment still loading the data from database, therefore the error occurs. 我遇到的问题是当方向改变时, mDbStudent被销毁但fragment仍然从数据库加载数据,因此发生错误。

I try to get the mDbStudent in the Fragment when onAttach , but when I move the pages very fast, it log stated I didn't close the mDbStudent 我试着在onAttach上获取Fragment onAttach ,但是当我快速移动页面时,日志声明我没有关闭mDbStudent

My question is Is there a way to handle the mDbStudent when orientation change? 我的问题是 ,当方向改变时,有没有办法处理mDbStudent

You'll need to instantiate your database outside of the Activity. 您需要在Activity之外实例化数据库。 Most likely, you'll be using the database in many Activies and Fragments, right? 最有可能的是,你将在许多活动和片段中使用数据库,对吧? You can create a static singleton, like this: 您可以创建一个静态单例,如下所示:

public class Global {
    private static Global instance = null;
    private Context context;

    public Global( Context context ) {
        this.context = context.getApplicationContext();
    }

    public static getInstance( Context context ) {
        if( instance == null )
            instance = new Global( context );
        return instance;
    }

    public SQLiteOpenHelper getDatabase() {
        return ... ;
    }
}

Then, in your Activity, you can call: 然后,在您的活动中,您可以致电:

Global.getInstance( this ).getDatabase()...

This stores the application's Context, so you can access that in Global instance whereever needed... even if the Activity has since been destroyed. 这将存储应用程序的Context,因此您可以在需要的时候在Global实例中访问它...即使Activity已被销毁。

This will also allow you to open the database once and keep it open, even through orientation changes. 这也允许您打开数据库一次并保持打开状态,即使通过方向更改也是如此。 In my apps, I generally leave the DB open until the app is closed explicitly. 在我的应用程序中,我通常会将数据库保持打开状态,直到应用程序显式关闭 If you have a single-Activity app, I would open the database, if closed, in onStart and close it in onUserLeaveHint() . 如果您有一个单活动应用程序,我会在onStart打开数据库(如果已关闭),并在onUserLeaveHint()关闭它。 This will only close the database if the use leaves the app, but not on orientation changes. 这只会在用户离开应用程序时关闭数据库,但不会在方向更改时关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM