Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I handle Orientation change so Fragment doesn't load?

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");
    }
 }
});
}
like image 692
AJW Avatar asked Oct 02 '15 07:10

AJW


People also ask

How do I stop android from restarting activity when changing orientations?

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.

How do I change the orientation of a fragment?

Use the following code line in the fragment where you want a specific (in this case portrait) orientation. getActivity(). setRequestedOrientation( ActivityInfo. SCREEN_ORIENTATION_PORTRAIT);

How do you prevent data from reloading and resetting when the screen is rotated?

Just add android:configChanges="orientation|screenSize" in activity tab of manifest file. So, Activity won't restart when orientation change.

How do you stop a fragment from recreating?

Calling setRetainInstance(true) will prevent the Fragment from being re-created.


2 Answers

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.

like image 60
Ivan V Avatar answered Oct 09 '22 18:10

Ivan V


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.

like image 44
NecipAllef Avatar answered Oct 09 '22 18:10

NecipAllef