0

This is the code for my fragment:

public class MainFragment extends Fragment {

private final AnimEffect forwardAnimation = AnimEffect.LEFT_TO_RIGHT;
private String couponNumber;
private EditText couponET;
@Override
public void onResume() {
    ((MainActivity) getActivity()).toggleBtn(false);
    super.onResume();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.mainfragment, container, false);
    ImageButton profile = (ImageButton) view.findViewById(R.id.Profile);
    ImageButton scan = (ImageButton) view.findViewById(R.id.Scan);
    Button validate = (Button) view.findViewById(R.id.Validate);
    couponET = (EditText) view.findViewById(R.id.couponET);

    OnClickListener profileListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            ((MainActivity) getActivity()).navigateTo(ProfileFragment.class, forwardAnimation);
        }
    };
    profile.setOnClickListener(profileListener);
    OnClickListener scanListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            ((MainActivity) getActivity()).navigateTo(ScannerFragment.class, forwardAnimation);
        }
    };
    scan.setOnClickListener(scanListener);

    OnClickListener validateListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            String response = couponET.getText().toString();
            if (response.contentEquals("ok")) {
                ((MainActivity) getActivity()).navigateTo(OKResponseFragment.class, forwardAnimation);
            }

        }
    };
    validate.setOnClickListener(validateListener);

    return view;
}

@Override
public void onStart() {
    super.onStart();
    if ((couponET != null) && (couponNumber != null)) {
        couponET.setText(couponNumber);
    }
}

public void setCouponNumber(String number) {
    couponNumber = number;
    if (couponET != null) {
        couponET.setText(number);
    }
}

public void alert() {
    final Dialog dialog = new Dialog(getActivity());
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.warningdialog);
    dialog.setCancelable(true);
    Button ok = (Button) dialog.findViewById(R.id.cancel);
    ok.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });

    dialog.show();
}

}

Now, the first time, the edit text works perfectly, i can use it, can write on it, anything. If i change the fragment, and then come back, i can't do nothing. the keyboard does not appear if i press the edit text. Even more, if i don;t close the keyboard, after i change the fragment, so that i have the keyboard on when i go back, if i press on the EditText and then write on the keyboard, nothing appears.

This is my FragmentActivity, if it helps:

public class MainActivity extends android.support.v4.app.FragmentActivity {

public static boolean scanner_active;

private Fragment f;

public static Intent login;

SharedPreferences settings;

private AnimEffect backPressAnimation = AnimEffect.LEFT_TO_RIGHT;

private ImageButton backButton;

@Override
public void onCreate(Bundle savedInstanceState) {
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    init();
    login = new Intent(MainActivity.this, WelcomeActivity.class);

}

private void init() {
    backButton = (ImageButton) findViewById(R.id.backButton);
    OnClickListener backButtonListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            Class<?> previousFragment = getPreviousScreen();
            if (previousFragment != null) {
                navigateTo(previousFragment, backPressAnimation);
            } else {
                finish();
                if(!Constants.remind){
                    startActivity(login);
                }
            }
        }
    };
    backButton.setOnClickListener(backButtonListener);
    String name;
    TextView nameTV = (TextView) findViewById(R.id.nameTV);
    if (Constants.remind) {
        settings = getSharedPreferences("PrefFile", 0);
        name = settings.getString("email", null);
    } else {
        name = Constants.email;
    }
    nameTV.setText(name);
    navigateTo(MainFragment.class, AnimEffect.FADE_IN);
}

@Override
public void onBackPressed() {
    Class<?> previousFragment = getPreviousScreen();
    if (previousFragment != null) {
        navigateTo(previousFragment, backPressAnimation);
    } else {
        finish();
        if(!Constants.remind){
            startActivity(login);
        }
    }
}

public void navigateTo(Class<?> fragmentClass, AnimEffect effect) {
    removeOldFragment(effect);
    int animIn = Util.getInAnimation(effect);
    try {
        f = (Fragment) fragmentClass.newInstance();
    } catch (Exception e) {
        LogService.err("navigate", e.getMessage(), e, LogService.LOG_LEVEL_LITE);
    }

    if (f != null) {
        getSupportFragmentManager().beginTransaction().disallowAddToBackStack().setCustomAnimations(animIn, 0).add(R.id.fragment, f, fragmentClass.getSimpleName()).commitAllowingStateLoss();
    }
}

public Class<?> getPreviousScreen() {
    if ((f instanceof MainFragment) && f.isAdded()) {
        return null;
    } else if ((f instanceof ProfileFragment) && f.isAdded()) {
        backPressAnimation = AnimEffect.LEFT_TO_RIGHT;
        return MainFragment.class;
    } else if ((f instanceof HelpFragment) && f.isAdded()) {
        backPressAnimation = AnimEffect.LEFT_TO_RIGHT;
        return MainFragment.class;
    } else if ((f instanceof ScannerFragment) && f.isAdded()) {
        backPressAnimation = AnimEffect.LEFT_TO_RIGHT;
        return MainFragment.class;
    } else if ((f instanceof OKUserResponseFragment) && f.isAdded()) {
        backPressAnimation = AnimEffect.LEFT_TO_RIGHT;
        return MainFragment.class;
    } else if ((f instanceof OKResponseFragment) && f.isAdded()) {
        backPressAnimation = AnimEffect.LEFT_TO_RIGHT;
        return MainFragment.class;
    }
    return null;
}

private void removeOldFragment(AnimEffect effect) {
    if ((f != null) && f.isAdded()) {
        int animOut = Util.getOutAnimation(effect);
        getSupportFragmentManager().beginTransaction().setCustomAnimations(0, animOut).remove(f).commitAllowingStateLoss();
        getSupportFragmentManager().executePendingTransactions();
    }
}

public void toggleBtn(boolean b) {
    backButton.setEnabled(b);
    if(b){
        backButton.setImageResource(R.drawable.back);
    }else{
        backButton.setImageResource(R.drawable.besmart2);
    }

}

public void setCouponText(String text) {
    // TODO Auto-generated method stub
    navigateTo(MainFragment.class, backPressAnimation);
    if ((f != null) && (f instanceof MainFragment)) {
        ((MainFragment) f).setCouponNumber(text);
    }
}

}

EDIT: it works on 4.x, but it doesn't work on previous OS like 2.3.5 or 3.2

rosu alin
  • 5,674
  • 11
  • 69
  • 150
  • Out of curiosity, if you set the `setFocusableInTouchMode` and `setFocusable` properties for the `EditText` in the `onResume` method of the `MainFragment` does the behavior persist? – user Nov 27 '12 at 18:10
  • I forgot to mention to set those methods to `true` but I guess you've done that. Have a look at this to try to bring the keyboard up. http://stackoverflow.com/questions/11025927/how-come-the-android-soft-keyboard-is-not-responding-to-edittext – user Nov 28 '12 at 09:28
  • i wanted to try that yesterday, but i get an error like: The method getSystemService(String) is undefined for the type new View.OnFocusChangeListener(){} – rosu alin Nov 28 '12 at 09:34
  • You need a reference of a `Context` for that. In a `Fragment` you have the `getActivity()` method for that: `getActivity().getSystemSevice...` – user Nov 28 '12 at 09:35
  • tried that, still no luck with the edittext. My code deletes previous fragments from memory, when u change them, so that it would not keep them all, to slow it down. I think the problem is something to do with this, the edittext is not initiated corectly the second time or something – rosu alin Nov 28 '12 at 10:53
  • If i create an intent and use startActivity instead of navigateTo() (code of function is upper), then it works, but i don't like the idea of reseting the activity everytime i change a fragment, any ideas what part of the code that is initiated at the activity start and not on the change of fragment should create this bug? – rosu alin Nov 28 '12 at 13:02

1 Answers1

0

I saw that if i press the validate button, event if the edittext doesnt work, an alert appears, and then if i dismiss the alert, it works. So what i did is, pass a boolean parameter to the alert, so that if is true, to call dialog.dismiss();. And i call on my onCreateView() the function:

alert(true);

and this creates an alert, and dismisses it fast, without showing it, making the edittext to work

rosu alin
  • 5,674
  • 11
  • 69
  • 150