Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I update a list view in one activity while im in another activity?

I currently have a tab layout with 2 tabs, one tab with a list view and one with the option make strings so I can add them in the list view. Both tabs have their own activity because this made the code much more structured, and I dont have to repeat my self later.

Lets say im in the tab that offer me to create an string, and i press the update list button, how do I update the list view without startActivity()? If i use startActivity(), it starts List.java, and instead of displaying the list in the list view tab, it takes full screen, which defies the purpose of the tab view. In other words, the startActivity() steals the focus from the tab view of the list, and sends it fulscreen.

Thanks for your time.

Edit: I want to update the activity in my list view tab, without starting a new activity that goes to fullscreen, and doesnt update the one in the tab.

like image 980
lands Avatar asked Jan 28 '26 20:01

lands


1 Answers

One solution is to have your data model separate from your view (Activity), this is good practice in general, but in this case allows your two tabs to interact with the same model.

You could provide access to the data model in your Application class. Then, when onResume is called on your list activity you can update the view based on the current data.

edit:

public class MyApplication extends Application {
   private List<MyObject> myData;

   public List<MyObject> getMyData() {
     return myData; 
   }

   public void setMyData(List<MyObject> mydata) {
     this.myData = myData;
   }
}

In your manifest you need to add the application tag to specify that it should use MyApplication as the application class.

Then, in your activity:

   public void onResume() {
      MyApplication app = (MyApplication) getApplication();
      List<MyData> data = app.getMyData();
      // update  my view with the data
   }
like image 167
Cheryl Simon Avatar answered Jan 30 '26 11:01

Cheryl Simon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!