Android: How to send a string data from one fragment to another fragment of the same main activity?

How to send a string URL from one fragment to another within the same activity.

Upvotes: 0

Views: 369

Answers (4)

H DA
H DA

Reputation: 53

you can create a shared ViewModel Object, fragments will communicate by observing the LiveData.

here is a basic example of LiveData and ViewModel, you can also find more information in here

Upvotes: 0

Konstantinos Nikoloutsos
Konstantinos Nikoloutsos

Reputation: 2180

The data that you want to pass to the fragment(According to your problem you should pass a string instad of int) :

Bundle bundle = new Bundle();
bundle.putInt(key, value);
fragment.setArguments(bundle);

Retrieve that information you need to get the arguments sent to the fragment.

Bundle bundle = this.getArguments();
if (bundle != null) {
    int i = bundle.getInt(key, defaulValue);
}

(*Found this answer here: Android: Pass data(extras) to a fragment)

Upvotes: 0

bkach
bkach

Reputation: 1441

A popular option now is to store the data in the ViewModel which can be shared by multiple activities / fragments.

From the Documentation:

This approach offers the following benefits:

  • The activity does not need to do anything, or know anything about this communication.
  • Fragments don't need to know about each other besides the SharedViewModel contract. If one of the fragments disappears, the other one keeps working as usual.
  • Each fragment has its own lifecycle, and is not affected by the lifecycle of the other one. If one fragment replaces the other one, the UI continues to work without any problems.

A good example can also be found in the documentation:

class SharedViewModel : ViewModel() {
    val selected = MutableLiveData<Item>()

    fun select(item: Item) {
        selected.value = item
    }
}

class MasterFragment : Fragment() {

    private lateinit var itemSelector: Selector

    private lateinit var model: SharedViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        model = activity?.run {
            ViewModelProviders.of(this).get(SharedViewModel::class.java)
        } ?: throw Exception("Invalid Activity")
        itemSelector.setOnClickListener { item ->
            // Update the UI
        }
    }
}

class DetailFragment : Fragment() {

    private lateinit var model: SharedViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        model = activity?.run {
            ViewModelProviders.of(this).get(SharedViewModel::class.java)
        } ?: throw Exception("Invalid Activity")
        model.selected.observe(this, Observer<Item> { item ->
            // Update the UI
        })
    }
}

Upvotes: 1

victorldavila
victorldavila

Reputation: 301

You have multiples ways to do that.

Below i've listed 3 options to do what you want:

  • You can use a bus, to comunicate between fragments. To do that you can use a lib called Eventbus(https://github.com/greenrobot/EventBus), or you can implement one using RxJava.
  • Another way to communicate between fragments, you can share an object in the activity that your fragments can access.
  • At last you can implement an event dispatcher em share between your scope.

Upvotes: 0

Related Questions