Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8611589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:24:49+00:00 2026-06-12T04:24:49+00:00

Good morning everyone, I’m having problems again in my proccess to create my first

  • 0

Good morning everyone,

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

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.

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();

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();
    }
}

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!!!.

Many thanks in advance!!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:24:50+00:00Added an answer on June 12, 2026 at 4:24 am

    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().

    for e.g.

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

    Hope this Helps..!! Cheers…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good morning. I've been having this issue for some days and have been lokking
Good morning everyone, I hope I'm not posting this in the wrong place; I've
Good Morning everyone, I'm totally confused with this problem. I have 3 UIWebViews and
Hello good morning everyone, I am developing an iphone app which provide push notification
Hello good morning everyone, i am having a weird issue when using imagePickerController. Actually
Hello good morning everyone, i have an iphone app which is still in development
Good morning everyone, I'm currently building a jquery step by step form at: http://jsfiddle.net/xSkgH/
Good morning everyone! I'm doing a distributed application using .NET 4.0, C# and Remoting
Hello good morning everyone. I am using asihttprequest for a series of images download
Good Morning How to create a new plist in iphone? it is possible please

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.