负载共享preferences在MainActivity和更新在次要活动 [英] Load SharedPreferences in MainActivity and Update in an secondary activity

查看:104
本文介绍了负载共享preferences在MainActivity和更新在次要活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家早上好,

我有一次在我的proccess创建我的第一个应用程序的问题。这一次与共享preferences文件。

I'm having problems again in my proccess to create my first app. This time with the SharedPreferences file.

我有必须使用相同的共享preferences文件2的活动。第一个是MainActivity,第二个用于数据的编辑布局

I have 2 activities that must use the same SharedPreferences file. The first one is the MainActivity and the second one an edit layout for the data.

在MainActivity我有以下的方法,我使用的数据连接到PLC:

In the MainActivity I have the following method where I use the data to connect to a PLC:

        //initialize the SharedPreferences variables for the PLC Data and Connection
    m_DataPLC = getSharedPreferences("CFTPreferences",CONTEXT_IGNORE_SECURITY);
    m_DataEditorPLC = m_DataPLC.edit();

    //Gets the number from the IP Address for the connection with the PLC
    //As default, the system takes the value of a static string value
    //This is when the SharedPreferences file is empty
    m_IpAddress = getResources().getString(R.string.ip_Address);
    m_NewIpAddress = m_DataPLC.getString("newIpAddress", m_NewIpAddress);
    if(m_NewIpAddress == null)
    {
        m_DataEditorPLC.putString("newIpAddress", m_IpAddress.toString());
        m_DataEditorPLC.commit();
        m_OldIpAddress = m_NewIpAddress = m_IpAddress;
    }
    else
    {
        m_OldIpAddress = m_IpAddress = m_NewIpAddress; 
    }

    //Start the connection with the PLC
    m_Connection = new ModbusConnection(this,m_IpAddress);
    inet = m_Connection.loginPLC();

在我的第二个活动,我必须加载相同的数据,并能对其进行修改。我做的第一个是登录到共享preferencesFile:

In my second activity, I have to load the same data and be able to modify it. What I do first is the login to the SharedPreferencesFile:

dataPLC = getSharedPreferences("CFTPreferences",CONTEXT_IGNORE_SECURITY);
dataEditorPLC = dataPLC.edit();

那我就写一个按钮,点击动作:

Then I do the writing with a click action of a button:

    public void setIPAddress()
{
    if (!newIpAddress.equals(etIPAddress.getText().toString()))
    {
        dataEditorPLC.putString("oldIpAdd",ipAddress.toString());
        dataEditorPLC.putString("newIpAdd",etIPAddress.getText().toString());
        dataEditorPLC.commit();
    }
}

我不知道如果我做错了调用同一个文件两次,或者如果我必须做一些额外的修补这一点。它看起来像它的更新,但它不会刷新MainActivity。如果有人有某种同样的问题,我想AP preciate在这一个帮助!

I dunno if I'm doing wrong calling the same file twice or if I have to do something extra to mend this. It looks like it does the update, but it doesn't refresh the MainActivity. If someone had some sort of the same problem, I would appreciate the help on this one!!!.

在预先感谢!

推荐答案

我觉得您访问的共享preferences的值的onCreate()第一项活动。这将是你的问题。当你从第二个活动回到第一个活动怎么一回事,因为,你的的onCreate()不叫,而不是 onResume()被调用。所以,更好的事情做的是移动,你访问共享preferences 值,以一个单独的函数code和调用这个函数在两个的onCreate() onResume()

I think you are accessing the value of Shared Preferences in onCreate() of first Activity. That will be your problem. Beacuse when you come back from second activity to first activity, your onCreate() is not called, instead onResume() is called. So the better thing to do is move the code where you access SharedPreferences value to a seperate function and call this function in both onCreate() and onResume().

对于例如。

public void getSharedPrefernces() {
  m_DataPLC = getSharedPreferences("CFTPreferences",CONTEXT_IGNORE_SECURITY);
  m_NewIpAddress = m_DataPLC.getString("newIpAddress", m_NewIpAddress);
}

希望这有助于..!干杯......

Hope this Helps..!! Cheers...

这篇关于负载共享preferences在MainActivity和更新在次要活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆