Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从活动中的片段保存数据_Android_Android Fragments_Android Activity_Save_Onsaveinstancestate - Fatal编程技术网

Android 从活动中的片段保存数据

Android 从活动中的片段保存数据,android,android-fragments,android-activity,save,onsaveinstancestate,Android,Android Fragments,Android Activity,Save,Onsaveinstancestate,在活动中保存片段中的数据将不起作用 设置:我有2个活动和2个动态创建的片段 当处于横向模式时,主活动包含两个片段, else仅对Main进行分段,并按ActivityForResult启动“活动编辑” 活动编辑包含片段编辑 主活动有一个数据库 Fragment Main从活动中获取一个列表 片段编辑修改列表 我的方法是在包含列表的Fragment Main被销毁时将列表保存在活动中 现状 调用ActivityForResult时,数据不会写入数据库 而且在横向模式下也不会 让我困惑的是,数据是在

在活动中保存片段中的数据将不起作用

设置:我有2个活动和2个动态创建的片段

当处于横向模式时,主活动包含两个片段, else仅对Main进行分段,并按ActivityForResult启动“活动编辑”

活动编辑包含片段编辑

主活动有一个数据库

Fragment Main从活动中获取一个列表

片段编辑修改列表

我的方法是在包含列表的Fragment Main被销毁时将列表保存在活动中

现状 调用ActivityForResult时,数据不会写入数据库 而且在横向模式下也不会

让我困惑的是,数据是在显示打开时写入的,但我在任何时候都不会更新MainActivity中的列表

 MainActivity 
================
override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        getTable() // gets Data from DataBase

        if (savedInstanceState == null) {
            supportFragmentManager.beginTransaction()
                .add(R.id.frame_main, MainFragment.newInstance(dbList, true), "mainFragment")
                .commit()

        } else {
            // Destroys the Fragment because I want to load it with different config
            var  mainFragment = supportFragmentManager.findFragmentByTag("mainFragment") as MainFragment
            mainFragment.onDestroyView()
            // Checks if it is Landscape
            if (TabletHelper.isLandscape(this)) {
                supportFragmentManager.beginTransaction().replace(
                    R.id.frame_main,
                    MainFragment.newInstance(dbList, false),
                    "mainFragment"
                ).commit()
                supportFragmentManager.beginTransaction().replace(
                    R.id.frame_edit,
                    EditFragment.newInstance(null, null, true, null),
                    "editFragment"
                ).commit()
            } else {
                supportFragmentManager.beginTransaction().replace(
                    R.id.frame_main,
                    MainFragment.newInstance(dbList, true),
                    "mainFragment"
                ).commit()
            }
        }

    }

// interface implementation from the MainFragment
    override fun passList(list: ArrayList<Product>) {
        dbList.clear()
        dbList.addAll(list)
    }
// Write the list to the DataBase
    override fun onDestroy() {
        super.onDestroy()
        productsDBHelper.writeAllProducts(dbList, TABLE_NAME_MAIN)
    }


}

 MainFragment
==============
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        if(savedInstanceState != null && savedState == null) {
            savedState = savedInstanceState.getBundle(BUNDLE)
        }
        if(savedState != null) {
            productList.addAll(savedState!!.getParcelableArrayList<Product>(PRODUCT_LIST_KEY)!!)
        }
        savedState = null

 //doing something with Layout...

        }
    }
// save bundel when onDestroyView is called 
   override fun onDestroyView() {
        super.onDestroyView()
        savedState = saveState()

    }
// Interface to pass the data to the Activity
    interface  OnFragmentMainClicked {
        fun passList(list: ArrayList<Product>)
    //..

    }


//creating Bundle for instance
    private fun saveState():Bundle{
        var state = Bundle()
        state.putParcelableArrayList(PRODUCT_LIST_KEY,productList)
        return state
    }
// Save Instance
    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        if (savedState != null ){
            outState.putBundle(BUNDLE,savedState)
        }else{
            outState.putBundle(BUNDLE,saveState())
        }
    }









//My Thought was to save the Data always when when onDestroyView in the Fragment is called
     override fun onDestroyView() {
        super.onDestroyView()
        savedState = saveState()
        onFragmentMainClicked.passList(productList)
    }
//But it is not working. Result is that it clears whole list
main活动
================
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
getTable()//从数据库获取数据
如果(savedInstanceState==null){
supportFragmentManager.beginTransaction()
.add(R.id.frame\u main,MainFragment.newInstance(dbList,true),“MainFragment”)
.commit()
}否则{
//销毁片段,因为我想用不同的配置加载它
var mainFragment=supportFragmentManager.findFragmentByTag(“mainFragment”)作为mainFragment
mainFragment.onDestroyView()
//检查它是否是横向的
if(表PER.isLandscape(本)){
supportFragmentManager.beginTransaction().replace(
R.id.frame_main,
MainFragment.newInstance(dbList,false),
“主要片段”
).commit()
supportFragmentManager.beginTransaction().replace(
R.id.frame_编辑,
EditFragment.newInstance(null,null,true,null),
“编辑片段”
).commit()
}否则{
supportFragmentManager.beginTransaction().replace(
R.id.frame_main,
MainFragment.newInstance(dbList,true),
“主要片段”
).commit()
}
}
}
//从MainFragment实现接口
覆盖有趣的密码列表(列表:ArrayList){
dbList.clear()
dbList.addAll(列表)
}
//将列表写入数据库
重写onDestroy(){
super.ondestory()
productsDBHelper.writeAllProducts(数据库列表,表名\u MAIN)
}
}
主要片段
==============
覆盖已创建的视图(视图:视图,保存状态:捆绑?){
super.onViewCreated(视图,savedInstanceState)
if(savedInstanceState!=null&&savedState==null){
savedState=savedInstanceState.getBundle(BUNDLE)
}
if(savedState!=null){
productList.addAll(savedState!!.getParcelableArrayList(产品列表键)!!)
}
savedState=null
//正在使用布局做一些事情。。。
}
}
//调用onDestroyView时保存bundel
重写onDestroyView(){
super.onDestroyView()
savedState=saveState()
}
//接口将数据传递给活动
单击FragmentMain上的界面{
趣味密码列表(列表:ArrayList)
//..
}
//例如,创建Bundle
私有存储状态():Bundle{
var state=Bundle()
state.putParcelableArrayList(产品列表键,产品列表)
返回状态
}
//保存实例
覆盖存储实例状态(超出状态:捆绑){
super.onSaveInstanceState(超出状态)
if(savedState!=null){
outState.putBundle(BUNDLE,savedState)
}否则{
outState.putBundle(BUNDLE,saveState())
}
}
//我的想法是在调用片段中的onDestroyView时始终保存数据
重写onDestroyView(){
super.onDestroyView()
savedState=saveState()
onFragmentMainClicked.passList(产品列表)
}
//但它不起作用。结果是它清除了整个列表

我不知道您为什么要首先在
onDestroyView()中设置列表。在活动激活之前,如果您想要此列表,请执行通信步骤。因此,要实现这一点,请创建另一个方法,在界面中获取列表
getList()
,该方法将返回列表。因为您必须同时实现
passList(list:list)
getList():list
。现在,在活动中全局获取一个列表对象,填充到
passList(list)
方法中,并返回全局列表对象(已经填充)。 然后用接口对象调用
getList()
。我希望您能使用该界面获得您的列表。
希望能有帮助。让我知道。

使用
接口
作为活动和片段之间的沟通者。这种做法对于开发来说非常好。我更新了代码。我正在使用一个界面,但何时使用它,以及为什么在我打开显示器时列表会更新。顺便说一句,谢谢你的回答。我解决了这个问题,但我不知道为什么它会这样工作。当我在片段中进行一些修改时,活动中作为ParcelableArrayList传递给片段的列表总是更新的。在“现在的活动”中,我总是在暂停时将列表写入数据库,以便不会丢失活动列表中的任何更改。因此,在我的情况下,界面是不需要的,但thx为您的想法很多。也许你可以解释为什么我通过参数传递给片段的列表也会覆盖活动中的列表。