0

I have a weird setup of fragments and editText views, and basically I need the fragment to be removed everytime a specific editText view is clicked.

This works, but only about 80% of the time.

The other 20% of the time where it fails, if you click it again, it 100% hides.

It's as if the first time you hit it, it doesn't have enough time to react and hide the fragment before the keyboard pops out.

My xml has Clickable set to true and onClick set to hide.

My java is here:

public void hide(View view) {

        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();

        try {
            transaction.remove(timerFragment);
            transaction.commit();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

Any ideas?

Gautam
  • 7,868
  • 12
  • 64
  • 105
EGHDK
  • 17,818
  • 45
  • 129
  • 204
  • Are you catching an exception on the times that it fails? – Tim Aug 03 '12 at 01:00
  • I'm not seeing anything in logcat when it fails, the only thing I see is everytime I click the editText view, it gives me an error in logcat. Doesn't matter whether or not the fragment is visible. `SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length`, but I don't know what that could be from, and I don't think it has anything to do with hiding the fragment. – EGHDK Aug 03 '12 at 01:06

3 Answers3

1

I ended up getting the problem resolved in another topic. I needed an onTouch method instead of an onClick. Look here: onClick event is not triggering | Android

Community
  • 1
  • 1
EGHDK
  • 17,818
  • 45
  • 129
  • 204
0

I hope it will helpful to you.

Try this..

    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);    

    final EditText ed1=(EditText)findViewById(R.id.editText1);
        final EditText ed2=(EditText)findViewById(R.id.editText2);



    ed1.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
        // TODO Auto-generated method stub
        call();
        return false;
        }
        });

    ed2.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
        // TODO Auto-generated method stub
        cal();
        return false;
        }
        });
}

public void call() {
        // TODO Auto-generated method stub
        Toast.makeText(getBaseContext(), "Hi", Toast.LENGTH_SHORT).show();
        //insert your codes here..
    }

    public void cal() {
        // TODO Auto-generated method stub
        Toast.makeText(getBaseContext(), "Hello", Toast.LENGTH_SHORT).show();
        //insert your codes here..
    }

My xml file is :

<EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
<requestFocus />
    </EditText>
Balaji
  • 2,024
  • 17
  • 13
  • Hmm... code works, but not always. I have two editText boxes, and if cursor is blinking in one, and I click on the other one (that has the onClickListener) it won't say "Hi" the first time. – EGHDK Aug 03 '12 at 06:05
0

It seems to me that you have two potential problems, and should decompose your effort to solve them one at a time. The first task is, verify that your click handler works 100% of the time. Once you do that, then look into verifying your fragment code is 100% reliable.

Regarding fragments:

I haven't seen anyone put a FragmentTransaction into a try block before, and the functions you are calling do not throw anything (according to the documentation). Is your catch block even reachable?

The name of your method is hide(). Would it be sufficient to show and hide your fragment, and not add or remove it?

Sparky
  • 8,437
  • 1
  • 29
  • 41