1

I have two modules of app: one for configuration settings and second - a game based on the configuration settings. In configuration app I choose language of the app (variable myLocal) and want to pass this variable to the second module. Do you know how to do that?
I just need to know if the user chose English (string 'en' or polish 'pl').

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
ctrone
  • 11
  • 2
  • Are they apps or module? If they are modules, probably you have a common module that should be a base module to others. You can communicate your modules via an event bus. The implementation of the bus should be in common and subscribers should be in derived modules. – nurisezgin Nov 15 '19 at 14:56
  • i dont know.. i run appConfig and then i can run appGame. I do not have any other modules/apps within this one. i thought these are called modules – ctrone Nov 15 '19 at 14:59

3 Answers3

0

Modules cannot talk to each other unless one is dependent on the other. It sounds like this is not the case, but your main app is dependent on both. If so, you should use the main app to talk between modules:

fun applyConfiguration() {
    val myLocal = module1.myLocal
    module2.applyLocale(myLocal)
}

Of course, that means your main app needs a way of being notified of changes made in module 1. That can be implemented in a variety of ways.

Edit To add one module as a dependency for another, go to Project Structure settings-->Dependencies. Click the + button under Declared Dependencies. Select Module Dependency and select your second module.

project_structure_settings

Chris
  • 1,180
  • 1
  • 9
  • 17
  • thanks, but where do I put this code? I only have module1 and module2, i dont have any other "main app" connecting them. first run module1 (configuration and then i can run module2 (simple game) – ctrone Nov 15 '19 at 14:07
  • So is `configuration` your main app then? If so, then it should have a dependency on module2 and can therefore talk to it directly. – Chris Nov 15 '19 at 14:09
  • but how? i cannt import the class from second module. i am new to this and did not find any tips in the internet. do I use any library/method/structure...? how can I find what I need? – ctrone Nov 15 '19 at 14:13
  • You need to add the second module as a dependency to the first. See my updated answer. – Chris Nov 15 '19 at 15:35
0

I assume these are two different applications. If you are starting the app from the configuration module, use a custom intent where you pass the language code such as PackageManager.getLaunchIntentFromPackage("com.example.mygame").putExtra("language", language) and from the target module get the "language" extra you sent with getIntent().getStringExtra("language").

If you are not starting the app within the configuration module, then there is something else you can do.

Define a broadcast receiver on your configuration app.

<receiver name=".MyConfigurationReceiver"
    android:enabled="true">
    <intent-filter>
        <action>com.example.configuration.MyCustomAction</action>
    </intent-filter>
</receiver>

Then, once you open the game (your game module), send a broadcast to this action with something like this which will be received from your configuration module:

getApplicationContext().sendBroadcast(new Intent(
    "com.example.configuration.MyCustomAction"));

Apply the same process on your game app as well, define a receiver with a different action:

<receiver name=".MyConfigurationFetcher"
    android:enabled="true">
    <intent-filter>
        <action>com.example.myapp.MyConfigurationFetchAction</action>
    </intent-filter>
</receiver>

After you receive the broadcast from .MyConfigurationReceiver on the first code, fetch the configuration from the module and send a broadcast back to the game. Note: This is from the configuration module. Example code:

@Override
public void onReceive(Context context, Intent intent)
{
    context.getApplicationContext().sendBroadcast(
         new Intent("com.example.myapp.MyConfigurationFetchAction")
         .putExtra("myConfiguration", Utilities.getConfiguration()))
}

That way, you can fetch the configuration from the .MyConfigurationFetcher receiver. You then can parse the configuration within your requirements. It may take time, but eventually you will get it as the operation is somewhat asynchronous.

This is just an example, but I think you get the idea.

Furkan Yurdakul
  • 2,801
  • 1
  • 15
  • 37
  • thanks! unfortunatey, getLaunchIntentFromPackage returns error: cannor resolve metod. do you have an idea why? should I do sth more? – ctrone Nov 15 '19 at 15:19
  • When I said `PackageManager.getLaunchIntentFromPackage()` I meant calling the package manager itself as an object, from an activity that would be `getPackageManager().getLaunchIntentFromPackage()` or from an any context `context.getPackageManager().getLaunchIntentFromPackage()`. – Furkan Yurdakul Nov 16 '19 at 16:43
0

If using navigation component, you can do something like this to pass the arguments to another module.

 val bundle = bundleOf("LOCAL_LANGUAGE" to language)
 findNavController().navigate(R.id.module2_navigation, bundle)

Here, I am using Kotlin's to function to pair left and right. You can refer to this for more info on passing data with bundle.

Note:

Safe Args do not support cross-module navigation, as there is no direct action to the destination

Anne
  • 259
  • 3
  • 4