0

I am trying to initialize a generic fragment in such manner that it can be used globally no matter what the instance of it is. For example:

 public MainActivity extends Activity{
    private Fragment fragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_layout);
        FragmentManager fragManager = getFragmentManager();
        FragmentTransaction transaction = fragManager.beginTransaction();

        if(condition1){
            fragment =  new MyFirstFragment();
            transaction
            .add(R.id.registrationFragment, fragment); //registrationFragment is FrameLayout
            transaction.commit();

        }else if(condition2){
            fragment = new MySecondFragment();
            transaction
            .add(R.id.registrationFragment, fragment); //registrationFragment is FrameLayout
            transaction.commit();
        }
        //create intent filter
        //create MyBroadcast object
        //registerReceiver(receiverObject, intentFilter)        
    }

    //Some action happens 
    class MyBroadcast extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();
        if (action.equals(action1)) {
            fragment.performAction();
        }



    }


}

public class MyFirstFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(
                R.layout.fragment_holder_a, container, false);
        //setWidgets etc...
        // TODO Auto-generated method stub
        return view;
    }

    //Used from main
    public void performAction(String text){
        //set some TextView's text from MainActivity event
        //different from MySecondFragment performAction method
    }

}

public class MySecondFragmentFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(
                R.layout.fragment_holder_b, container, false);
        //setWidgets etc...
        // TODO Auto-generated method stub
        return view;
    }

    //Used from main
    public void performAction(String text){
        //set some TextView's text from MainActivity event
        //different from MyFirstFragment performAction method
    }
}

The problem is that in my real application I may have more than 2 Fragments that the Fragment object could become an instance of. I am trying to avoid a lot of if statements to check which fragment instance was inflated with the FragmentManager. Also the problem I am facing is that When i declare the Fragment fragment = new MyFirstFragment(); I cannot access the fragment.performAction("Stringssss") method. This is not the solution I am looking for:

public MainActivity extends Activity{
    private MyFirstFragment myFirstFrag;
    private MySecondFragment mySecondFrag;
    private MyThirdFragment myThirdFrag;
    private MyNthFragment myNthFrag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_layout);
        FragmentManager fragManager = getFragmentManager();
        FragmentTransaction transaction = fragManager.beginTransaction();

        if(condition1){
            myFirstFrag =  new MyFirstFragment();
            transaction
            .add(R.id.registrationFragment, myFirstFrag); //registrationFragment is FrameLayout
            transaction.commit();

        }else if(condition2){
            mySecondFrag = new MySecondFragment();
            transaction
            .add(R.id.registrationFragment, mySecondFrag); //registrationFragment is FrameLayout
            transaction.commit();
        }else if(condition3){
            //...
        }else if(conditionNth){
            //...
        }
        //create intent filter
        //create MyBroadcast object
        //registerReceiver(receiverObject, intentFilter)        
    }

    //Some action happens 
    class MyBroadcast extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();
        if (action.equals(action1)) {
            if(null != myFirstFrag)
                myFirstFrag.performAction();
            }else if(null != mySecondFrag){
                mySecondFrag.performAction();
            }else if(null != myThirdFrag){
                myThirdFrag.performAction();
            }else if(null != myNthFrag){
                myNthFrag.performAction();
            }
        }else if(action.equals(actionNTH)){
            if(null != myFirstFrag)
                myFirstFrag.performAction();
            }else if(null != mySecondFrag){
                mySecondFrag.performAction();
            }else if(null != myThirdFrag){
                myThirdFrag.performAction();
            }else if(null != myNthFrag){
                myNthFrag.performAction();
            }
        }



    }


}

As you can see in the BroadcastReceiver a lot of If-else statements start to pile up making the code clunky and dirty. I am looking for a clean solution that will not require me to have a bunch of if-else statements and keep the code simple.

Thank you.

TerNovi
  • 390
  • 5
  • 16
  • As the fragment you use it's determined in a single output if-else group why don't you have a single reference to that fragment? You could then make all those fragments extend a single abstract base fragment class(the type of that single reference) in which the performAction() method will be placed. In the BroadcastReceiver you'll just make a simple function call on the base fragment reference. – user Aug 25 '15 at 16:15
  • I tried extending the fragment to a BaseFragment. But on the BaseFragment, I kept getting null when I tried to reference the widget contained in the Fragment itself. – TerNovi Aug 25 '15 at 18:13

0 Answers0