5

When an EditText line in the UI gains focus, a DatePickerFragment launches for the user to enter a date.

On orientation change, if the EditText line has focus and a previously entered date (so that length() > 0) I don't want the fragment to automatically show.

Is there a way to modify or add code so that if the Activity is newly created and the EditText line has focus it won't automatically launch the DatePickerFragment?

Would it be a good idea to do something in onResume()?

Or do something with savedInstanceState() and !=null?

public class Activity extends AppCompatActivity {
...
EditText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
...
    if (hasFocus && (EditText.getText().length() == 0)) {
       DatePickerFragment newFragment = new DatePickerFragment();
       newFragment.show(getSupportFragmentManager(), "datePicker");
    }
    else if (hasFocus && (EditText.getText().length() > 0)) {
       ...
       DatePickerFragment newFragment = new DatePickerFragment();
       newFragment.show(getSupportFragmentManager(), "datePicker");
    }
 }
});
}
David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
AJW
  • 1,578
  • 3
  • 36
  • 77

2 Answers2

4

Do you use viewPager or just add fragment into your activity? One way is to save data that you want to maintain on rotation into onSaveInstanceState(Bundle outState) bundle and then restore that data inside onCreate using

 if(savedInstanceState != null) {
   //get your data
 }

or simply add your fragment only when the activity was loaded (not reloaded after device rotation)

if(savedInstanceState == null) 
{
    mFragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();

    YourFragment fragment = new YourFragment();

    fragmentTransaction.add(R.id.fragment_container, fragment);
    fragmentTransaction.commit();
}

Anyways you should definitely check this and this questons, they might be helpful.

Community
  • 1
  • 1
Ivan V
  • 3,024
  • 4
  • 26
  • 36
  • Hi, just adding fragment in the activity. I will edit above to show the Activity. – AJW Oct 02 '15 at 07:36
  • Ok, so I am using DatePickerDialog so show(getSupportFragmentManager) does the fragmenttransactions. The "if(savedInstanceState == null)" line can also be interpreted as don't add the fragment when the savedInstanceSate is not null, which is true after device rotation? – AJW Oct 02 '15 at 07:48
  • yes, even if you don't save any data to outState bundle, savedInstenceState will be not null if device rotation is the case – Ivan V Oct 02 '15 at 07:50
  • Excellent, that works. Also, since I have the length test "if (hasFocus && (EditText.getText().length() == 0))" do I later need to explicitly include "length()>0" as shown above or will that always be the case if length is not ==0 so therefore length>0 would not be needed? – AJW Oct 02 '15 at 07:57
  • if you want to create fragment only if it was device rotation AND the edit text is not empty, then you should use it again – Ivan V Oct 02 '15 at 08:01
3

If I were you, instead of using a OnFocusChangeListener I would use a OnClickListener to show the fragment whenever EditText is clicked, and make EditText non-focusable.

For the orientation change, it is worth noting that you can prevent configuration changes taking effect on an activity by adding android:configChanges="orientation" on the manifest to the related activity. Not sure if this helps in your case though, but you can take a look.

NecipAllef
  • 486
  • 1
  • 5
  • 18
  • adding android:configChanges="orientation" is good to prevent activity from restart. but it won't work if you only need to prevent fragment from re-creation, not the whole activity – Ivan V Oct 02 '15 at 07:54
  • Thanks for your note. But some users will have a keyboard where they can use the tab key rather than a mouseclick on the EditText. If I make non-focusable then user won't be able to move focus onto the EditText using their tab key. – AJW Oct 02 '15 at 07:54
  • I forgot to mention that my Activity has OnClickListener code too, as you recommended. Thanks! – AJW Oct 02 '15 at 08:51