0

I created a tabbed Activity and I have to modify some views from the MainActivity class. How can I get access to any of these three fragments created from that Activity?

I know the method involving the use of getFragmentManager().findFragmentById(R.id.example_fragment);, but I don't know where I can find fragment ID.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • Possible duplicate of [Android - Set fragment id](https://stackoverflow.com/questions/9363072/android-set-fragment-id) – Masoom Badi Apr 01 '19 at 20:41
  • @Sam. It's not the same question at all, in my opinion. Firstly, I asked for "How to find" that ID, but not "How to set" it. Secondly, I search for a method to modify fragment views from activity. For example - setting the text in TextView but from the Activity. – unkownStudent Apr 01 '19 at 22:43
  • if you read that question correctly it says _How can I set a Fragment's Id so that I can use getSupportFragmentManager().findFragmentById(R.id.--)?_ in order to find you need to set it somewhere.. scroll the answer's its all there – Masoom Badi Apr 01 '19 at 23:22

1 Answers1

0

While doing the fragment transaction you might consider setting an id by yourself and later you can retrieve the Fragment using that id as well.

Here's how you set up and id/tag for your Fragment which is being launched.

getFragmentManager().beginTransaction().add(R.id.fragment_container, new YourFragment(), "FRAGMENT_1").commit();

Now you can retrieve the Fragment using the id (i.e. tag) like the following.

getFragmentManager().findFragmentByTag("FRAGMENT_1");

Now to answer your other question, which is how you can access a view component of your Fragment from your Activity class, you can do this in multiple ways.

One possible option of doing this is having a BroadcastReceiver in your Fragment which can be invoked to update views inside the Fragment while the broadcast is received from your Activity. Here's a sample Fragment with a BroadcastReceiver to explain the idea behind.

public class YourFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        // Start listening for the broadcast sent from the Activity
        LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mYourBroadcastReceiver, new IntentFilter("MESSAGE_FROM_ACTIVITY"));

        return inflater.inflate(R.layout.your_fragment_layout, null, true);
    }

    @Override
    public void onDestroyView() {
        // The BroadcastReceiver must be destroyed when your Fragment is being destroyed
        LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mYourBroadcastReceiver);
    }

    private final BroadcastReceiver mYourBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // This will be invoked when a broadcast is received from your Activity
            // Update your views here
        }
    };
}

Now send the broadcast from your Activity like this when needed.

Intent intent =new Intent("MESSAGE_FROM_ACTIVITY");
sendBroadcast(i);

Hope that helps!

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • Okay, thanks a lot, but it's too difficult for me to understand 'BroadcastReceiver'. What about setting up a tag for 'Fragment' - I've got error on 2nd argument. "Found: 'com.tesing.fragmentapp.MyFragment', required: 'android.app.Fragment'". My Fragment class extends 'Fragment' and everything seems to be correct. – unkownStudent Apr 01 '19 at 22:36
  • Please check if `MyFragment` extends the `support.v4.Fragment`. Look into the imports of your `MyFragment` class. Use the `Fragment` instead of `support.v4.Fragment`. – Reaz Murshed Apr 01 '19 at 22:44
  • This advice causes error, because I created app from a scratch and everywhere is imported only `support.v4.Fragment` and now I'm getting error `getItem(int)' in 'com.testing.example.MainActivity.SectionsPagerAdapter' clashes with 'getItem(int)' in 'android.support.v4.app.FragmentPagerAdapter'; attempting to use incompatible return type` – unkownStudent Apr 01 '19 at 23:00
  • Then you need to use getSupportFragmentManager instead of getFragmetManager – Reaz Murshed Apr 01 '19 at 23:06
  • I tried everything I could try and I get nothing (As I noticed, `findFragmentByTag()` returns `null`). It's a way easier to declare view from `onCreateView()` as `static` and get memory leakage and break Instant Run than make this code work correctly. – unkownStudent Apr 02 '19 at 19:33