• 14

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

How to update not showing fragments

I am developing an android app wich receives information from a local server time to time. For the in-app navigation I chose to use a navigation drawer wich is replacing the selected fragments with

getSupportFragmentManager().beginTransaction().replace(R.id.flContent, fragment).addToBackStack(null).commit();

adding the selected fragment to the backStack for proper BackPress navigation.

The fragments are initialized in my MainActivity in

  private OverviewFragment overviewFragment;
  private IncidentFragment incidentFragment;
  private SettingsFragment settingsFragment;

protected void onCreate(){
  overviewFragment = new OverviewFragment();
  incidentFragment = new IncidentFragment();
  settingsFragment = new SettingsFragment();

What I want to do now is to update the TextViews out of the information received from the server. I tried this with the folling method:

public void setIncidentDetails(NetworkIncident incident) {
        if (incident != null) {
            ArrayList<String> medicNames = null;

            ((TextView) view.findViewById(R.id.inLocationText)).setText(incident.getLocation());
            ((TextView) view.findViewById(R.id.inKeywordText)).setText(incident.getKeyword());
            if(incident.isEmsCalled()) {
                ((TextView) view.findViewById(R.id.inEmsText)).setText("Rettungsdienst alarmiert");
            } else {
                ((TextView) view.findViewById(R.id.inEmsText)).setText("Kein Rettungsdienst alarmiert");
            for (int i = 0; i < incident.getMedics().size(); i++){
                medicNames = new ArrayList<String>();
                NetworkMedic currentMedic = incident.getMedics().get(i);
                medicNames.add(currentMedic.getForename() + " " + currentMedic.getSurename());
            ((TextView) view.findViewById(R.id.inMedicText)).setText(medicNames.toString());

This only works if I previously opened fragment containing the TextViews. Otherwise I get a NullPointerException on the "view.findViewById...". Obviously because it hasn't been initialized before ;)

My question is if there is another way to update the TextViews if the fragment is not showing (another fragment is selected in the NavigatinoDrawer) or if hasn't even been selected.

(Maybe some global resource file from wich the fragment pulls its information onCreate / onResume)?

    • Whenever your required fragment is in Foreground then fetch the data from your local server and try to update the TextViews you want. It is not allowed to update UI of Activity/Fragment when it is background.
    • Save and Fetch data to and from Local DB is also one of the solution. Save and fetch data in lifecycle methods of fragments.

Thanks to @Tejas I could solve this question. You have to, indeed use a DB or store the data-to-update temporarily to update a currently non-showing fragment.

As soon as this fragment is brought into foreground you can retrieve the stored data to update your TextViews etc...

  • 1
Reply Report