Change element in an Application

0

I am working with the altbeacon library using beacons background I use it to get events with firebase, when this background notifies the events that are scheduled on the same day, when it is open shows the events in a recycler view, I need that when you leave the beacon region change the events screen, but from a class that extends from Application I can not access the elements with findViewById, try with

 View v = View.inflate(getApplicationContext(),R.layout.activity_event_list,null);

inside didExitRegion but any changes you make with v.findViewById.set something does not work I had to create a static method in the Activity and use a static activity

private static Activity activity = null;
protected void onCreate(Bundle savedInstanceState) {
    ....
    activity = this;
....
 public static void changeList() {
    if (activity==null){
        return;
    }

    RecyclerView rv = activity.findViewById(R.id.lista_eventos);
    if (rv != null) {
        if (NotificationService3.estado) {
            rv.setVisibility(View.VISIBLE);
        } else {
            rv.setVisibility(View.GONE);
        }
    }
}

then within the method of the exitExit and the onCreate of the Activity I call the static function, I also tried to pass a method to the method to create the view with an inflate, by parameter but either called in the activity or in the application it does not result

    
asked by jultrun 14.09.2018 в 01:43
source

1 answer

0

Hello, instead of using static methods, I recommend you use an interface.

You create an interface so that with one or two methods and you have the Activity implement the interface, then you pass that interface to the Application from the Activity. Finally, when it falls in the didExitRegion you call the method you need from that interface.

The Application should look something like this:

public class MyApplication exteds Application {

   public RegionInterface mRegionInterface;

   @Override
   public void didExitRegion(Region arg0) {
      if (mRegionInterface!=null) mRegionInterface.regionChanged();
   }   

   public interface RegionInterface {
      void regionChanged();
   }
}

And in the activity you would have to put this:

public class MyActivity extends Activity implement RegionInterface {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      MyApplication myApplication = (MyApplication)getApplication();
      myApplication.mRegionInterface = this;
   }

   @Override
   protected void onDestroy() {
      super.onDestroy();
      //Super importante para que no explote
      MyApplication myApplication = (MyApplication)getApplication();
      myApplication.mRegionInterface = null;
   }

   @Override
   public void regionChanged(){
      //Aca pones el codigo que necesites para cambiar las vistas del activity
   }  
}

Using this you can always have a communication between the Application and the Activity without problem, not only for this specific case but whenever you need to pass some data or catch any event.

    
answered by 14.09.2018 / 19:20
source