-1

I'm sure this is simple but I'm new to Android development and can't find the answer.

On my main screen I have a number of TextViews that I update when data arrives - this works fine when the app first starts.

The problem arises when I open a menu to change a setting. On returning from the menu the TextViews no longer update.

I see that onCreate is called when I return from the menu and so I'm guessing the problem is that the references to the TextViews are being changed however as I'm using the reference it doesn't make sense that the TextView doesn't update.

In onCreate I have:

txtStartPower = (TextView) findViewById(R.id.txtPowerStart);

then to update I use:

txtStartPower.setText(Integer.toString(startW));

In my AndroidManifest.xml for the settings I have:

<activity android:name=".SettingsActivity"
        android:parentActivityName=".MainActivity"
        android:label="Settings">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />
</activity>

and in the menu selection code I have:

public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case R.id.action_settings:
            Intent sIntent = new Intent(this, SettingsActivity.class);
            startActivity(sIntent);
            return true;
    }
    return(super.onOptionsItemSelected(item));
}

Any help please...

Acuario
  • 11
  • 4

3 Answers3

0

This might be for a number of reasons:

Are you sure the network task that you are running is executing in a background thread? Make sure all of your network based tasks are in the background thread, if not this might cause the UI to stop responding as the network task is being performed in the main thread and it is blocking the UI from being updated.

If your text view is in a fragment, it might be the case that the updates are being passed to the old reference of the activity instead of the newly resumed one. Note that such errors are commonly associated with AsyncTasks. Without seeing any more code I cant comment, but if you are using AsyncTask and are updating your views in the onPostExecute then this might lean on being the issue of your activity losing its connection to the fragment. Your fragment is updating the old activity and it does not have the reference to the new activity. This can be solved by setting the retain instance method of fragment to true.

More info regarding these can be obtained from here:

https://developer.android.com/reference/android/app/Fragment.html#setRetainInstance(boolean)

http://www.mobiledeveloperguide.com/android/using-asynctask-and-fragments.html

Hope this helps.

shiredude95
  • 560
  • 3
  • 7
  • Yes the network activity is still running. It is reading messages from mqtt and displays a toast notification message when data is received. – Acuario Apr 13 '18 at 06:13
  • Yes the network activity is still running. It is reading messages from mqtt and displays a toast notification message when data is received. The structure is pretty basic - all the TextViews are in activity_main.xml and the code is all in MainActivity. I have a method DisplayValues that is called to update all the TextViews, this is being called. In onCreate I retrieve the references for the TextViews. When a mqtt message is received its data is used to populate the TextViews. All works well until I open a menu view and return. The mqtt continues to run and receive/process but no updates. – Acuario Apr 13 '18 at 06:19
  • Can you try updating the view from inside onResume. Also instead of updating it with data, just try populating it with some dummy string. Also where are you calling the mqtt from? An async task or some kind of a service? – shiredude95 Apr 13 '18 at 06:22
  • Updating from onResume works but only once for the data that has changed while the menu is shown. Subsequent data updates do not update the TextView although they call my update method. The mqtt is started and runs in the main activity. I did originally have problems when returning from menus that a second instance of the mqtt was being started, I fixed this with a separate class that holds system status and variables that has a boolean to check if the mqtt is started or not. – Acuario Apr 13 '18 at 16:43
  • In response to the answer you have posted below, the back button is called the HomeButton You can enable it by following this: https://developer.android.com/training/implementing-navigation/ancestral.html#NavigateUp However for this to work, you must declare the activity you are entering the settings from as the parent activity in the manifest. You might also want to change the launch mode of the activity based on what works for you. This thread talks about it: https://stackoverflow.com/questions/14462456/returning-from-an-activity-using-navigateupfromsametask/16147110#16147110 – shiredude95 Apr 13 '18 at 22:40
  • Thanks for the pointers shiredude95 - as per my original question, in the end the answer is yes, there is a simple fix. Add android:launchMode="singleTop" to the MainActivity and it works as expected. – Acuario Apr 14 '18 at 04:16
  • Please consider upvoting my og post if you found it helpful. – shiredude95 Apr 14 '18 at 04:17
0

Normally if you want to update view corresponding to other activity(in your case SettingsActivity), you should implement onActivityResult. It is general solution to use "startActivityForResult() and onActivityResult()" when you want communication between activities. Try to find about this will help. And my guess, onCreate should not called because your view already created but onResume will called. You can also study about "Android lifecycle".

Wooram Jung
  • 228
  • 2
  • 10
0

**See update below

I have an answer that solves the problem but sadly loses a nice feature.

When I use the menu to open the settings there is a friendly back arrow in the top left corner. Using this to go back and the TextViews don't update anymore. Using the arrow causes the onCreate method to be called as well as the onResume method. This results in no updates.

If I use the back key instead of the arrow then onCreate doesn't get called and the updates work as expected.

The arrow is displayed when I have the following in my AndroidManisfest.xml file

    <activity android:name=".SettingsActivity"
        android:parentActivityName=".MainActivity"
        android:label="Settings">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />
    </activity>

Removing several 'bits' from the AndroidManifest.xml to leave this

    <activity android:name=".SettingsActivity"
         android:label="Settings">
    </activity>

and I sadly lose the back arrow but when I go back using the back button only onResume gets called and the TextViews refresh.

Maybe there is a way/setting that adds the back arrow without causing onCreate to run. Anyone know?

**UPDATE Finally yes, there is a simple fix. Add android:launchMode="singleTop" to the MainActivity (shown below) and it works as expected.

    <activity android:name=".MainActivity"
        android:launchMode="singleTop">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
Acuario
  • 11
  • 4