Android connection between actions without onActivityResult

In the application in which I am currently working, I ran into the problem of communication between actions.
I basically have a UI component that is similar to Combobox. However, the list of possible values ​​for this component should be opened in a new one Activity.
Therefore, when you click on a component, a new one opens Activityusing startActivityForResult.
When you select a new value, it is placed inside Bundleand returns to the previous one Activity. Then I need to use the method onActivityResultto get the selected value and set it to the component.
This means that everyone Activitywho uses this component must override onActivityResultand update the component with a new value.
Instead, I want the component to take care of all these things, and you need to register Listener, like you, for TextViewand similar components.
But at the moment, I just can’t find a good way to do this, because the message is closely related to Activity, and I just can’t get the result Activityout onActivityResult.
Does anyone know a good solution to this problem?

+4
source share
6 answers

How about using BroadCastReceiver ?

, , , , onReceive

, , :

   <receiver android:name=".TestBroadCast">
      <intent-filter>
         <action android:name="io.test.TEST"/>
      </intent-filter>
   </receiver>

BroadCastReceiver

public class TestBroadCastReceiver extends BroadcastReceiver {

   @Override
   public void onReceive(Context context, Intent intent) {
      //TODO: Handle the Intent received.

}

, :

   public static final String INTENT_ACTION = "io.test.TEST";
   public static final String INTENT_EXTRA  = "someData";

   Intent intent = new Intent();
   intent.setAction(INTENT_ACTION);
   intent.putExtra(INTENT_EXTRA,"test");
   sendBroadcast(intent);

, , :)!

UPDATE ~

, , :

IntentFilter intentFilter = new IntentFilter("io.test.TEST");
TestBroadCastReceiver mReceiver = new TestBroadCastReceiver();
context.registerReceiver(mReceiver, intentFilter);

P.S. LocalBroadcastReceiver, , , Common BroadcastReceiver

+1

EventBus - . , onActivityResult.

: - - - .sendSticky().

, - - - , , :  )  ) - ( ) onResume() - .onPause()

, , /

+1

Observer, - BroadcastReceiver , Otto

+1

EventBus https://github.com/greenrobot/EventBus 3 , 3- PartyLibrary. .

0

, .

, 2 (.. MainActivity)

public static ArrayList arrayList ;

public static SparseBooleanArray sparseBooleanArray ;

, , MainActivity.arrayList. , sparseBooleanArray .

arrayList sparseBooleanArray, , ,

arrayList.add(1,"List Item 1") ;

sparseBooleanArray.put(1,false ) ; // represent selected value for List Item 1

, , arraylist , SparseBooleanArray .

0

Source: https://habr.com/ru/post/1661412/


All Articles