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");
}
}
});
}
If you want the activity to not restart during screen orientation change, you can use the below AndroidManifest. xml. Please note the activity android:configChanges=”orientation|screenSize” attribute. This attribute makes the activity not restart when change screen orientation.
Use the following code line in the fragment where you want a specific (in this case portrait) orientation. getActivity(). setRequestedOrientation( ActivityInfo. SCREEN_ORIENTATION_PORTRAIT);
Just add android:configChanges="orientation|screenSize" in activity tab of manifest file. So, Activity won't restart when orientation change.
Calling setRetainInstance(true) will prevent the Fragment from being re-created.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With