2

I am having a problem with the interaction between an Activity and a Fragment.

I have a main Activity and an into Activity with layout buttons and text inputs. When I open the Fragment in the main Activity, I can still click on a button in the Activity and open the Fragment again, or enter text in the text inputs.

I've been searching for help two hours, and I haven't found the answer!

This is how I open the Fragment from the Activity:

FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            SignUp fragment = new SignUp();
            fragmentTransaction.add(R.id.fragment_content, fragment);
            fragmentTransaction.commit();

And this is picture: picture

jjm
  • 6,028
  • 2
  • 24
  • 27
  • Just remove the listener from the button once it is clicked once and the fragment is added (if I understand what you are saying) – zgc7009 Mar 02 '15 at 20:35
  • ok, but what to do with input fields? I can enter text into input field on activity layout from fragment. – ZoranSmederevac Mar 02 '15 at 20:38
  • Your question isn't really that clear with the information your provided. Your picture doesn't really show anything and your code just says you are adding a fragment to an activity. If you don't want your fields to be editable/focusable when the fragment is built just remove any event listeners. – zgc7009 Mar 02 '15 at 20:41
  • This answer explains how to disable input fields: http://stackoverflow.com/a/8942552/916451 – jjm Mar 02 '15 at 20:42
  • Buttons can be disabled with the `setEnabled(boolean)` method. – jjm Mar 02 '15 at 20:43
  • It might be more effective to use two fragments, one with the controls that are currently in the activity, and the fragment you currently have. The button in the first fragment can tell the activity to show the second. – jjm Mar 02 '15 at 20:44
  • And yeah, your picture doesn't show the whole screen. – jjm Mar 02 '15 at 20:45
  • i thought that fragment auto freeze activity when it open. So i should disable buttons and inputs on activity when i open fragment, and when fragment is removed, i must to enable buttons and inputs, right? – ZoranSmederevac Mar 02 '15 at 20:47

2 Answers2

2

I have faced the same problem and searched for hours to find a solution or the reason and I find it.

When you add a fragment to an activity, the functionally of the fragment and activity both will respond to the touch on the screen.

If you want to not touch through the fragment simply create an empty setOnTouchListener in the onViewCreated function of the fragment and it is done.

Code in Java

@Override
protected void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(v, savedInstanceState);
    view.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            return true;
        }
    });
}

Code in Kotlin

override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    view!!.setOnTouchListener { view, motionEvent -> return@setOnTouchListener true }
}
Andrew Hossam
  • 381
  • 4
  • 12
0

Thx to all. I found solution like a jjm say:

"It might be more effective to use two fragments, one with the controls that are currently in the activity, and the fragment you currently have. The button in the first fragment can tell the activity to show the second."

I just create new fragment with component of activity and replace fragments. But i still don't understand why we can click button on activity layout through fragment layout? Why is it usefull?