0

I'm very new to Androind, trying to figure how fragment and activity should be work together. I have a very ugly layout. 1 activity and 1 "root" fragment. When user click on left menu fragment are replaced by fragment manager.

expandableList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
    @Override
    public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {

        LeftMenuItem group = groups.get(i);
        String fragmentTag = group.getFragmentTag();
        if (fragmentTag.equals(Fragment1.TAG)) {
            Fragment1 fragment = (Fragment1) currentFragmentManager.findFragmentByTag(Fragment1.TAG);
            if (fragment == null) {
                fragment = new Fragment1();
            }
            FragmentTransaction ft = currentFragmentManager.beginTransaction();
            ft.replace(R.id.root_frame, fragment, Fragment1.TAG);
            ft.commitAllowingStateLoss();

        } else if (fragmentTag.equals(Fragment2.TAG)) { 

I assume code above is supposed to replace current fragment with new one. Fragments are always null actually. I do not know why.

In onCreateView of RootFragment Fragment1 is created by default.

    if (savedInstanceState == null) {
        Log.d(TAG, "savedInstanceState is null, creating Framgent1");

        Fragment1 fragment = new Fragment1();
        FragmentTransaction ft = currentFragmentManager.beginTransaction();
        ft.replace(R.id.root_frame, fragment, Fragment1.TAG);
        ft.commitAllowingStateLoss();
    }

In onCreateView of rootFragment, rootFragment replaces itself with another Fragment1, which looks very ugly for me. Is it well known Androind pattern or just bad design ?

Let's assume that'm sending httpRequest from onCreateView of MyActivity using Volley. Once I received response I need to update Fragment1 UI from callback. How can I do it ?

  1. Should I try to find fragment using findFragmentByTag in my activity and update UI directly ? Is http volley response is in the same thread ? If no, it is ok to update UI from different thread ?

  2. Should I use a Handler class to send message from Activity to Fragment ?

user12384512
  • 3,362
  • 10
  • 61
  • 97

1 Answers1

1
  1. Should I try to find fragment using findFragmentByTag in my activity and update UI directly ? Is http volley response is in the same thread ? If no, it is ok to update UI from different thread ?

findFragmentByTag will not be useful since as soon as replace is called, the previous fragment is destroyed. If you have only few fragment you want to switch, you can use hte below solution :

How can I switch between two fragments, without recreating the fragments each time?

Answer to second part of your question 1 :

Volley response is always called on the main thread to its perfectly ok to update UI on the callback. You should never update UI elements on any thread other than MAIN / UI thread.

  1. Should I use a Handler class to send message from Activity to Fragment ?

You can choose to send the message by handler but if usually android documentation suggests to send communicate between activity and attached fragments via callbacks

https://developer.android.com/training/basics/fragments/communicating.html

Community
  • 1
  • 1
Arpit Ratan
  • 2,976
  • 1
  • 12
  • 20