Hello, I started writing an application for Android and I had a question, how to transfer data from one Activity to two at once? I will describe my task more precisely, I have the first MainActivity , by clicking on the button another VremActivity with a legacy from the tabActivity

Here is the code:

 public class VremActivity extends TabActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.vrem); // ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ TabHost TabHost tabHost = getTabHost(); // инициализация Π±Ρ‹Π»Π° Π²Ρ‹ΠΏΠΎΠ»Π½Π΅Π½Π° Π² getTabHost // ΠΌΠ΅Ρ‚ΠΎΠ΄ setup Π²Ρ‹Π·Ρ‹Π²Π°Ρ‚ΡŒ Π½Π΅ Π½ΡƒΠΆΠ½ΠΎ TabHost.TabSpec tabSpec; tabSpec = tabHost.newTabSpec("tag1"); tabSpec.setIndicator("Π’ΠΊΠ»Π°Π΄ΠΊΠ° 1"); tabSpec.setContent(new Intent(this, OneActivity.class) ); tabHost.addTab(tabSpec); tabSpec = tabHost.newTabSpec("tag2"); tabSpec.setIndicator("Π’ΠΊΠ»Π°Π΄ΠΊΠ° 2"); tabSpec.setContent(new Intent(this, TwoActivity.class)); tabHost.addTab(tabSpec); } } 

This activit displays two OneActivity ΠΈ TwoActivity in tabs OneActivity ΠΈ TwoActivity So, with the MainActiviry you need to transfer data to OneActivity ΠΈ TwoActivity . Thank you in advance.

    1 answer 1

    Define something like an event in the application class, subscribe to it in MainActivity, and in OneActivity and TwoActivity call this event. The application is available in all activities via getApplication ();

    those. eg:

    Define classes:

     class MyEvent { public void fire(String someParam) { } } class MyEventListener { private ArrayList<MyEvent> _events = new ArrayList<MyEvent>(); public void addEvent(MyEvent event) { _events.add(event); } public void fireEvents(String param){ for(MyEvent event: _events) { event(param); } } } 

    In the application class (If you have not created it, then you need to create it)

    ...

      private MyEventListener _eventListener = new MyEventListener(); public MyEventListener getEventListener(){ return _eventListener; } 

    ...

    In the main activation, before the call (once), you hang up the event handler like this:

     ((MyApplication)getApplication()).getEventListener().addEvent( new MyEvent(){ public void fire(String param){ if(param=="data1"){ }else if(param=="data2"){ } } }); 

    ...

    In your activations for data transfer, write: ...

     ((MyApplication)getApplication()).getEventListener().fireEvents(myParams); 

    ...

    • Sorry, but could you in more detail, but I began to study Android quite recently and did not understand how to implement it, I have not yet come across this. - st1nger757
    • Well, the android has nothing to do with it. What exactly is not clear to you? - Chad
    • Well, you said to define an event in the application class, I do not know how to do it. - st1nger757
    • Similarly, doing the opposite, i.e. in the second and third activations you add your event, and in the main one you activate fireEvents (...) - Chad