0

I'm new Android programming. Earlier I was working with activities, where i could implement onClick on an ImageButton and go to a different activity.

Now I need to do the same but using Fragments. I have a prototype with a menu that always appear on screen and can show different activities to the user. The different lactivities would be inside this container.

Now I want to place an ImageButton inside a fragment and make that the screen shows the next fragment. But I'm confused how to do it.

I have the following components:

  • Activity_main(java)+activity_main.xml (with menu)
  • Fragment1(java)+fragment1.xml(working normal) Inside this layout I have an ImageButton and want to show Fragment2
  • Fragment2(java)+fragment2.xml

How should look Fragment1 to can call Fragment2? I will be glad if the answer could be the clearest possible because I'm new on it, and maybe I could forgot an obvious step. Thanks

Singorenko
  • 473
  • 2
  • 7
  • 19
  • as described in [docs](https://developer.android.com/training/basics/fragments/communicating.html) all communications between fragments must be performed through activity – nnesterov Jan 11 '17 at 11:41
  • Possible duplicate of [Basic Communication between two fragments](http://stackoverflow.com/questions/13700798/basic-communication-between-two-fragments) – Nouman Shah Jan 11 '17 at 11:53

3 Answers3

0

Simply make a method in your activity which will always change/replace fragment when you invoke it. something like

public void updateFragment(Fragment fragment){
//add fragment replacing code here
}

in your fragment, invoke it some thing like this

((YourActivity)getActivity()).updateFragment(new YourFragment());

since, it is just an idea which works fine but still you can improve the logic.

Junaid Hafeez
  • 1,618
  • 1
  • 16
  • 25
0

Actually, going from one fragment to another is almost similar to going from one activity to another. There are just a few extra lines of code. First, add a new Java class named SingleFragmentActivity which would contain the following code-

public abstract class SingleFragmentActivity extends AppCompatActivity
{
    protected abstract Fragment createFragment();

    @LayoutRes
    protected int getLayoutResId()
    {
        return R.layout.activity_fragment;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutResId());

        FragmentManager fm = getSupportFragmentManager();

        Fragment fragment = fm.findFragmentById(R.id.fragment_container);

        if (fragment == null)
        {
            fragment = createFragment();
            fm.beginTransaction().add(R.id.fragment_container, fragment).commit();
        }
    }
}

Make your activities in the following format-

public class SomeActivity extends SingleFragmentActivity
{
    @Override
    protected Fragment createFragment()
    {
        return SomeFragment.newInstance();
    }
}

And your fragments like this-

public class SomeFragment extends Fragment
{
    public static SomeFragment newInstance()
    {
        return new SomeFragment();
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View v = inflater.inflate(R.layout.fragment_some, container, false);

        return v;
    }
}

After this everything has the same code as you have for activities except for one small detail which is your onCreateView(LayoutInflater, ViewGroup, Bundle) class. This is how you would write it-

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View v = inflater.inflate(R.layout.fragment_some, container, false);

    mTextView = (TextView)v.findViewById(R.id.some_text);
    mButton = (Button)v.findViewById(R.id.some_button);

    mTextView.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            check();
        }
    });

    return v;
}

And that is it!

  • @baldcl If no answer actually _answered_ your question, comment on the answers and ask for clarification –  Jan 12 '17 at 07:17
0

Hi i hope you are already aware about the fragments and their uses but still here is a brief. They are child to an activity and an activity can have more than one fragment so you can update your layout without changing activity just by changing fragments.

You can found more on fragment here : https://developer.android.com/training/basics/fragments/index.html

Back to the original problem, supposed you are in MainActivity.java and you want to load fragment in it, so you do this to load fragment first time.

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        transaction.replace(R.id.frame, new Fragment1);
        transaction.addToBackStack(null);
        transaction.commit(); 

You will need this method to change fragment from another fragment, so add this in your MainActivity

 public void changeFragment(Fragment fragment){
        FragmentTransaction transaction =     getSupportFragmentManager().beginTransaction();

        transaction.replace(R.id.frame, new Fragment1);
        transaction.addToBackStack(null);
        transaction.commit();
    }

Now from a button click in this fragment

 button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

          ((MainActivity)getActivity()).changeFragment(new Fragment2);

        }
    });

Hope it will help!

Sid
  • 13
  • 2
  • 12