DialogFragment dismisses itself after orientation change - android

A similar question has been asked before, but my case is different.
I have DialogFragments all over my app. When I rotate the phone, all of the DialogFragments come back without issue except this one.
I've littered the life cycle callbacks with Log messages to see what is going on, and this is the scenario:
My DialogFragment is created and shown
On rotation, I save whatever I want to into a bundle for restoration afterwards.
DialogFragment is successfully recreated. I know because onCreate through to onResume are called.
Immediately after resumption, for some inexplicable reason, onPause, onStop, onDestroyView, onDestroy and onDetach are called in rapid succession. The DialogFragment is destroyed immediately after recreation and I don't know why.
Any help is much appreciated. The DialogFragment starts an activity for result to take a picture. It works well for most phones, but the Galaxy S3 camera causes orientation changes that force the activity to be recreated. I don't mind this, I know how to handle activity recreation, but this I've never encountered.
The DialogFragment is started via a RecyclerView adapter callback from a regular fragment, in the main hosting activity.
I do not show the DialogFragment using the ChildFragmentManger in the fragment hosting the RecyclerView because multiple fragments can show this DialogFragment and the function is always the same. It was much more prudent to have the activity receive the callback regardless of which fragment started it.
From the fragment:
selectionPickAdapter.setAdapterListener(new selectionPickAdapter.AdapterListener() {
#Override
public void onSelectionClicked(Selection selection) {
if (getActivity() instanceof RankingActivity) {
((RankingActivity) getActivity()).onSelectionClicked(selection);
}
}
});
The main hosting activity receives the call back and shows it thus:
#Override
public void onSelectionClicked(Selection selection) {
if (isSignedInUser) {
selectionsToEdit.put(selection.hashCode(), selection);
if (baseCategory != null) {
selection.setCategory(baseCategory);
}
RankingSelectionEditDialogFragment rankingSelectionEditDialogFragment =
RankingSelectionEditDialogFragment.newInstance(SELECTION_EDIT, selection.hashCode(), selection);
rankingSelectionEditDialogFragment.show(getSupportFragmentManager(), EDIT_TAG);
}
else {
Intent i = new Intent(this, BusinessActivity.class);
i.putExtra(Constants.BUSINESS, selection.getBusiness().getId());
startActivity(i);
}
}
These are my lifecycle callbacks:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
selectionToEdit = savedInstanceState.getParcelable(SELECTION_TO_EDIT);
imagePath = savedInstanceState.getString(IMAGE_PATH);
}
else {
selectionToEdit = getArguments().getParcelable(SELECTION_TO_EDIT);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (savedInstanceState != null) {
Log.i(TAG, "CREATED A SECOND TIME!");
}
else {
Log.i(TAG, "CREATED ONCE!");
}
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_ranking_edit, container, false);
initializeViewComponents(rootView);
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setupFragment(selectionToEdit);
}
#Override
public void onResume() {
super.onResume();
Log.i(TAG, "onResume");
Dialog dialog = getDialog();
if (dialog != null) { // Only do this if returning a dialog, not a fragment
Log.i(TAG, "Dialog is not null");
SharedPreferences sharedPreferences
= getActivity().getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE);
// Get items required to put dialog just under the ActionBar.
int screenWidth = sharedPreferences.getInt(Constants.SCREEN_WIDTH, 720);
int screenHeight = sharedPreferences.getInt(Constants.SCREEN_HEIGHT, 1280);
int screenDPI = sharedPreferences.getInt(Constants.SCREEN_DPI, 320);
Window window = dialog.getWindow();
window.setLayout(screenWidth, WindowManager.LayoutParams.WRAP_CONTENT);
WindowManager.LayoutParams windowLayoutParams = window.getAttributes();
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
windowLayoutParams.y = -((screenHeight / 2) - 56) * (screenDPI / 160);
window.setAttributes(windowLayoutParams);
if (dialog.isShowing()) {
Log.i(TAG, "Dialog is showing");
}
else {
Log.i(TAG, "Dialog is not showing");
}
}
else {
Log.i(TAG, "Dialog is null");
}
Log.i(TAG, "onResume finished");
}
/**
* The system calls this only when creating the layout in a dialog.
*/
#Override
#NonNull
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
// The only reason you might override this method when using onCreateView() is
// to modify any dialog characteristics. For example, the dialog includes a
// title by default, but your custom layout might not need it. So here you can
// remove the dialog title, but you must call the superclass to get the Dialog.
SharedPreferences sharedPreferences
= getActivity().getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE);
// Get items required to put dialog just under the ActionBar.
int screenWidth = sharedPreferences.getInt(Constants.SCREEN_WIDTH, 720);
int screenHeight = sharedPreferences.getInt(Constants.SCREEN_HEIGHT, 1280);
int screenDPI = sharedPreferences.getInt(Constants.SCREEN_DPI, 320);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
Window window = dialog.getWindow();
window.setLayout(screenWidth, WindowManager.LayoutParams.WRAP_CONTENT);
WindowManager.LayoutParams windowLayoutParams = window.getAttributes();
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
windowLayoutParams.y = -((screenHeight / 2) - 56) * (screenDPI / 160);
windowLayoutParams.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(windowLayoutParams);
return dialog;
}
/**
* Restore the previous currentFragment before the dialog was brought up
*/
#Override
public void dismiss() { // Used when the user deliberately dismisses the dialog
Log.i(TAG, "Dismissed");
super.dismiss(); // Ensure Super class method is called
}
/**
* Restore the previous currentFragment before the dialog was brought up
*/
#Override
public void onCancel(DialogInterface dialog) { // Used when the user inadvertently leaves the dialog,
// e.g back pressed or touched outside the dialog
Log.i(TAG, "Cancelled");
super.onCancel(dialog); // Ensure Super class method is called
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable(SELECTION_TO_EDIT, selectionToEdit);
outState.putString(IMAGE_PATH, imagePath);
}
#Override
public void onPause() {
Log.i(TAG, "onPause");
super.onPause();
}
#Override
public void onStop() {
Log.i(TAG, "onStop");
super.onStop();
}
#Override
public void onDestroyView() {
Log.i(TAG, "View Destroyed");
super.onDestroyView();
}
#Override
public void onDestroy() {
Log.i(TAG, "onDestroy");
super.onDestroy();
}
#Override
public void onDetach() {
Log.i(TAG, "onDetach");
super.onDetach();
}
EDIT: I've fixed the issue. To show the DialogFragment, I should use the ChildFragmentManager of the hosting fragment and not the activity. That is, changing this:
RankingSelectionEditDialogFragment rankingSelectionEditDialogFragment =
RankingSelectionEditDialogFragment.newInstance(SELECTION_EDIT, selection.hashCode(), selection);
rankingSelectionEditDialogFragment.show(getSupportFragmentManager(), EDIT_TAG);
to this:
RankingSelectionEditDialogFragment rankingSelectionEditDialogFragment =
RankingSelectionEditDialogFragment.newInstance(SELECTION_EDIT, selection.hashCode(), selection);
switch (currentFragment) {
case CATEGORY_PICK:
rankingCategoryPickFragment = (RankingCategoryPickFragment)
getSupportFragmentManager().findFragmentByTag(CATEGORY_PICK_TAG);
if(rankingCategoryPickFragment != null) {
rankingSelectionEditDialogFragment.show
(rankingCategoryPickFragment.getChildFragmentManager(), EDIT_TAG);
}
break;
case BUSINESS_SORT:
rankingBusinessSortParentFragment = (RankingBusinessSortParentFragment)
getSupportFragmentManager().findFragmentByTag(BUSINESS_SORT_TAG);
if(rankingBusinessSortParentFragment != null) {
rankingSelectionEditDialogFragment.show
(rankingBusinessSortParentFragment.getChildFragmentManager(), EDIT_TAG);
}
break;
was the ticket. Hope that helps anybody else with a similar issue.

Related

How to check if the custom DialogFragment is displayed?

1) In my application, the user may receive a lot of notifications from FCM
2) If the user has an application open, he needs to display the custom DialogFragment
3) If the DialogFragment is already displayed, then the next time the notification arrives, it is necessary to prohibit the repeated display of this DialogFragment
4) My dialogue code:
public final class NotificationEventDialog extends DialogFragment implements DialogInterface.OnKeyListener, View.OnClickListener {
private Activity mCurrentActivity;
private NotificationEventDialogListener mNotificationEventDialogListener;
public interface NotificationEventDialogListener {
void showEvent();
}
public NotificationEventDialog() {
}
public static NotificationEventDialog newInstance() {
NotificationEventDialog notificationEventDialog = new NotificationEventDialog();
notificationEventDialog.setCancelable(false);
return notificationEventDialog;
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
mCurrentActivity = (Activity)context;
try {
mNotificationEventDialogListener = (NotificationEventDialogListener) mCurrentActivity;
} catch (ClassCastException e) {
throw new ClassCastException(mCurrentActivity.toString() + " must implemented NotificationEventDialogListener");
}
}
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
LayoutInflater inflater = LayoutInflater.from(mCurrentActivity);
#SuppressLint("InflateParams") View view = inflater.inflate(R.layout.dialog_notification_event, null);
Button btnNotificationEventYes = view.findViewById(R.id.notification_event_dialog_yes);
btnNotificationEventYes.setOnClickListener(this);
Button btnNotificationEventNo = view.findViewById(R.id.notification_event_dialog_no);
btnNotificationEventNo.setOnClickListener(this);
AlertDialog.Builder builder = new AlertDialog.Builder(mCurrentActivity);
builder.setView(view);
return builder.create();
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
if (getDialog() != null && getDialog().getWindow() != null) {
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().setOnKeyListener(this);
}
return super.onCreateView(inflater, container, savedInstanceState);
}
#Override
public void onDetach() {
super.onDetach();
mCurrentActivity = null;
mNotificationEventDialogListener = null;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.notification_event_dialog_yes:
dismiss();
mNotificationEventDialogListener.showEvent();
break;
case R.id.notification_event_dialog_no:
dismiss();
break;
}
}
#Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
dismiss();
return true;
} else return false;
}
}
5) Each time I receive a notification from FCM, I create a dialog box:
DialogFragment notificationEventDialog = NotificationEventDialog.newInstance();
notificationEventDialog.show(getSupportFragmentManager(), "");
6) How to check if DialogFragment is already displayed? Every time I create a new object of this window and I cannot make it as Singleton, because This leads to a memory leak.
Found an answer in which a person suggests using Weak links to solve this problem:
Also you can store a weak link to the shown dialog in that singletone
class. Using such method, you can detect is your dialog currently
shown or not.
There was also such an answer:
I suggest to save link to the dialog in single instance class. In that
instance create method ensureShowDialog(Context context). That method
would check is current shown dialog or not. If yes, you can show the
dialog. In another casr you can pass new data you to the dialog.
But, honestly, I can’t quite understand how to use these tips in practice. Please can help with this realization or suggest another way? Thanks in advance.
You can use:
val ft: FragmentTransaction = fragmentManager!!.beginTransaction()
val prev: Fragment? = fragmentManager!!.findFragmentByTag("typeDialog")
if (prev == null) {
val fm = fragmentManager
val courseTypeListDialogFragment =
CourseTypeListDialogFragment()
courseTypeListDialogFragment.isCancelable = false
courseTypeListDialogFragment.setStyle(
DialogFragment.STYLE_NO_TITLE,
0
)
courseTypeListDialogFragment.setTargetFragment(this, 1)
if (fm != null) {
courseTypeListDialogFragment.show(ft, "typeDialog")
}
}
You can check if dialog fragment is showing by calling isAdded () inside DialogFragment or by
DialogFragment notificationEventDialog = NotificationEventDialog.newInstance();
notificationEventDialog.isAdded()
from activity
It will return true if fragment is added to an Activity, in case of dialog fragment - is shown.
You can store last shown dialog fragment date via putting System.currentTimeMillis() in SharedPreferences
I think you'v got the idea.

Count Down Timer app has issue when I change orientation

The layout of my app contains a TextView and a toggle Button. When the toggle Button is turned ON an AlertDialog appears and the user is prompted to give the time for the countdown to start. It works fine if I dont change the orientation while it counts down. However when I change orientation while the countdown keeps running the Dialog Box reappears which shouldn't. I know that changing orientation destroys and recreates my activity so given the fact that toggle button was ON before the activty is destroyed when it is recreated it continuous to be ON as it should be. So my question is if there is a way for the AlertDialog not to appear after the orientation change.
I have tried adding the following but it didnt work
Declared as class variable
public static final String TOGGLE_BUTTON_STATE = "OFF";
Trying to set the toggle Button to true
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: created.............");
mTextTime = (TextView) findViewById(R.id.textView);
mToggleButton = (ToggleButton) findViewById(R.id.toggleButton);
if((savedInstanceState != null) && TOGGLE_BUTTON_STATE.equals("ON")) {
Log.d(TAG, "onCreate: created after changing orientation........");
mToggleButton.setChecked(true);
}
mToggleButton.setOnCheckedChangeListener(this);
saving the state before it is destroyed
#Override
public void onSaveInstanceState(Bundle outState) {
if(mToggleButton.isChecked()) {
Log.d(TAG, "onSaveInstanceState: toggleButton is checked...........****");
outState.putString(TOGGLE_BUTTON_STATE, "ON");
}else {
Log.d(TAG, "onSaveInstanceState: toggleButton is not checked...........*****");
outState.putString(TOGGLE_BUTTON_STATE, "OFF");
}
super.onSaveInstanceState(outState);
}
//Listener for the ToggleButton
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// Toast.makeText(this, "ON", Toast.LENGTH_SHORT).show();
// TOGGLE_BUTTON_ON = true;
//getting the xml user_input to java
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.user_input, null);
//search inside the view for the text_input
mTextUserInput = (EditText) view.findViewById(R.id.text_input);
//We create the builder and we use it to add functionality to the dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Please Enter The Time");
//We create the user_input that has only the editext widget that we gonna use to get the
//time from the user
builder.setView(view);
builder.setPositiveButton("OK", this);
builder.setNegativeButton("Cancel", this);
builder.show();
} else {
// OFF selected and timer must stop
// TOGGLE_BUTTON_ON = false;
timer.stop();
}
}
ps The countdown timer keeps running properly even after orientation change
Your way of loading the previously stored state in onCreate is false. You are saving the state correctly (but i would prefer storing it as a boolean) - but you are not reading it correctly from the savedInstance.
The way i would do it:
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("TOGGLE_BUTTON_STATE", mToggleButton.isChecked());
}
#Override
protected void onCreate(Bundle savedInstanceState) {
...
if(savedInstanceState != null) {
mToggleButton.setChecked(savedInstanceState.getBoolean("TOGGLE_BUTTON_STATE"));
}
mToggleButton.setOnCheckedChangeListener(this);
...
}
You can dismiss the alert dialog when the activity is going to be destroyed.
For example:
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
...
// Save the dialog in an instance variable
mDialog = builder.show();
}
#Override
public void onStop() {
if (mDialog != null) {
mDialog.dismiss();
}
super.onStop();
}

Android DialogFragment not showing when show() called in onCreate of Activity

I have a dialog fragment with a simple indeterminate progress bar in the centre, which i use to show network activity:
public class NativeLoadingDialogFragment extends DialogFragment {
public NativeLoadingDialogFragment() {
// Blank
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = new Dialog(getActivity(), android.R.style.Theme_Dialog);
ProgressBar indeterminateProgressBar = new ProgressBar(getActivity());
indeterminateProgressBar.setIndeterminate(true);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
dialog.setContentView(indeterminateProgressBar);
dialog.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
return dialog;
}
public boolean isShowing() {
return getDialog() != null;
}
}
I have used the dialog fragment throughout my app with no issues, it shows up without issue in lots of places when i call dialog.show(getFragmentManager, null), however when I try to call it in onResume of my settings activity it does not show!
I have an activity for settings, which launches the system settings to change the language of the phone. Once the user changes the language and my activity resumes I detect if the language has changed and do a network call:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
mLoading = new NativeLoadingDialogFragment();
if (savedInstanceState != null) {
if (savedInstanceState.containsKey(EXTRA_LANGUAGE)) {
String language = savedInstanceState.getString(EXTRA_LANGUAGE);
String currentLanguage = AppUtils.getDefaultLanguageCode(
SmartBankConstant.DEFAULT_LANGUAGE,
SmartBankConstant.SUPPORTED_LANGUAGES);
if (!language.equals(currentLanguage)) {
updateLanguage(Language.stringToLanguage(currentLanguage));
}
}
}
}
private void updateLanguage(Language newLanguage) {
....
getSpiceManager().execute(new SetLanguageRequest(newLanguage),
new SetLanguageRequestListener(this));
mLoading.show(getFragmentManager(), null);
getFragmentManager().executePendingTransactions();
}
The code definitely runs but no dialog appears! If the network call fails I have a retry option that calls the updateLanguage(Language newLanguage) method again, and the dialog actually appears that time! What am I doing wrong?
Try with this approach. It checks if the dialog is already displayed, otherwise it shows it.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
mLoading = new NativeLoadingDialogFragment();
if (savedInstanceState != null) {
...
}
mLoading.show(getFragmentManager(), null);
}
private void updateLanguage(Language newLanguage) {
...
if (mLoading != null && !mLoading.isVisible()) {
mLoading.show(getFragmentManager(), null);
getFragmentManager().executePendingTransactions();
}
}
I don't know why, but running fragment transaction in the next loop helped to solve this issue.
new Handler().post(new Runnable() {
#Override public void run() {
// for some reason this must be called in the next loop
dialogFragment.show(getSupportFragmentManager(), tag);
}
});

How to retain EditText data on orientation change?

I have a Login screen which consists of 2 EditTexts for Username and Password. My requirement is that on orientation change , input data(if any) in EditText should remain as it is and a new layout should also be drawn. I have 2 layout xml files- one in layout folder and other in layout-land folder. I am trying to implement following 2 approaches but none of them is perfect:
(1) configChanges:keyboardHidden - In this approach, I don't provide "orientation" in configChanges in manifest file. So I call setContentView() method in both onCreate() and onConfigurationChanged() methods. It fulfills both my requirements. Layout is changed and input data in EditTexts also remains as it is. But it has a big problem :
When user clicks on Login button, a ProgressDialog shows until server-response is received. Now if user rotates the device while ProgressDialog is running, app crashes. It shows an Exception saying "View cannot be attached to Window." I have tried to handle it using onSaveInstanceState (which DOES get called on orientation change) but app still crashes.
(2) configChanges:orientation|keyboardHidden - In this approach, I provide "orientation" in manifest. So now I have 2 scenarios:
(a) If I call setContentView() method in both onCreate() and onConfigurationChanged(), Layout is changed accordingly but EditText data is lost.
(b) If I call setContentView() method in onCreate() , but not in onConfigurationChanged(), then EditText data is not lost but layout also not changes accordingly.
And in this approach, onSaveInstanceState() is not even called.
So I am in a really intimidating situation. Is there any solution to this problem? Please help. Thanx in advance.
By default, Edittext save their own instance when changing orientation.
Be sure that the 2 Edittexts have unique IDs and have the same IDs in both Layouts.
That way, their state should be saved and you can let Android handle the orientation change.
If you are using a fragment, be sure it has a unique ID also and you dont recreate it when recreating the Activity.
A better approach is to let android handle the orientation change. Android will automatically fetch the layout from the correct folder and display it on the screen. All you need to do is to save the input values of the edit texts in the onSaveInsanceState() method and use these saved values to initialize the edit texts in the onCreate() method.
Here is how you can achieve this:
#Override
protected void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.login_screen);
...
...
String userName, password;
if(savedInstanceState!=null)
{
userName = savedInstanceState.getString("user_name");
password= savedInstanceState.getString("password");
}
if(userName != null)
userNameEdtTxt.setText(userName);
if(password != null)
passEdtTxt.setText(password);
}
>
#Override
protected void onSaveInstanceState (Bundle outState)
{
outState.putString("user_name", userNameEdtTxt.getText().toString());
outState.putString("password", passEdtTxt.getText().toString());
}
Give the element an id and Android will manage it for you.
android:id="#id/anything"
in onConfigurationChanged method, first get the data of both the edit texts in global variables and then call setContentView method. Now set the saved data again into the edit texts.
There are many ways to do this. The simplest is 2(b) in your question. Mention android:configChanges="orientation|keyboardHidden|screenSize" in your manifest so that Activity doesn't get destroyed on Orientation changes.
Call setContentView() in onConfigChange(). but before calling setContentView() get the EditText data into a string and set it back after calling setContentView()
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mEditTextData = mEditText.getText().tostring();//mEditTextData is a String
//member variable
setContentView(R.layout.myLayout);
initializeViews();
}
private void initializeViews(){
mEditText = (EditText)findViewById(R.id.edittext1);
mEdiText.setText(mEditTextData);
}
The following should work and is standard to the activities and fragments
#Override
public void onSaveInstanceState (Bundle outState)
{
outState.putString("editTextData1", editText1.getText().toString());
outState.putString("editTextData2", editText2.getText().toString());
super.onSaveInstanceState(outState);
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate();
... find references to editText1, editText2
if (savedInstanceState != null)
{
editText1.setText(savedInstanceState.getString("editTextData1");
editText2.setText(savedInstanceState.getString("editTextData2");
}
}
Im restoring instance to restore values and it works fine for me :)
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.addtask2);
if(savedInstanceState!=null)
onRestoreInstanceState(savedInstanceState);
}
Remove android:configChanges attribute from the menifest file and let android handle the orientation change your data in edittext will automatically remain.
Now The problem you mentioned is with the progress dialog force close this is because when the orientation is changed the thread running in backgroud is trying to update the older dialog component whihc was visible. You can handle it by closing the dialog on savedinstancestate method and recalling the proceess you want to perform onRestoreInstanceState method.
Below is a sample hope it helps solving your problem:-
public class MyActivity extends Activity {
private static final String TAG = "com.example.handledataorientationchange.MainActivity";
private static ProgressDialog progressDialog;
private static Thread thread;
private static boolean isTaskRunnig;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate");
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new EditText.OnClickListener() {
#Override
public void onClick(View v) {
perform();
isTaskRunnig = true;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void perform() {
Log.d(TAG, "perform");
progressDialog = android.app.ProgressDialog.show(this, null,
"Working, please wait...");
progressDialog
.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
//isTaskRunnig = false;
}
});
thread = new Thread() {
public void run() {
Log.d(TAG, "run");
int result = 0;
try {
// Thread.sleep(5000);
for (int i = 0; i < 20000000; i++) {
}
result = 1;
isTaskRunnig = false;
} catch (Exception e) {
e.printStackTrace();
result = 0;
}
Message msg = new Message();
msg.what = result;
handler.sendMessage(msg);
};
};
thread.start();
}
// handler to update the progress dialgo while the background task is in
// progress
private static Handler handler = new Handler() {
public void handleMessage(Message msg) {
Log.d(TAG, "handleMessage");
int result = msg.what;
if (result == 1) {// if the task is completed successfully
Log.d(TAG, "Task complete");
try {
progressDialog.dismiss();
} catch (Exception e) {
e.printStackTrace();
isTaskRunnig = true;
}
}
}
};
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.d(TAG, "onRestoreInstanceState" + isTaskRunnig);
if (isTaskRunnig) {
perform();
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.d(TAG, "onSaveInstanceState");
if (thread.isAlive()) {
thread.interrupt();
Log.d(TAG, thread.isAlive() + "");
progressDialog.dismiss();
}
}
As pointed out by Yalla T it is important to not recreate the fragment. The EditText will not lose its content if the existing fragment is reused.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_frame);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// Display the fragment as the main content.
// Do not do this. It will recreate the fragment on orientation change!
// getSupportFragmentManager().beginTransaction().replace(android.R.id.content, new Fragment_Places()).commit();
// Instead do this
String fragTag = "fragUniqueName";
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = (Fragment) fm.findFragmentByTag(fragTag);
if (fragment == null)
fragment = new Fragment_XXX(); // Here your fragment
FragmentTransaction ft = fm.beginTransaction();
// ft.setCustomAnimations(R.xml.anim_slide_in_from_right, R.xml.anim_slide_out_left,
// R.xml.anim_slide_in_from_left, R.xml.anim_slide_out_right);
ft.replace(android.R.id.content, fragment, fragTag);
// ft.addToBackStack(null); // Depends on what you want to do with your back button
ft.commit();
}
Saving state = Saving (Fragment State + Activity State)
When it comes to saving the state of a Fragment during orientation change, I usually do this way.
1) Fragment State:
Save and Restore EditText value
// Saving State
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("USER_NAME", username.getText().toString());
outState.putString("PASSWORD", password.getText().toString());
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.user_name_fragment, parent, false);
username = (EditText) view.findViewById(R.id.username);
password = (EditText) view.findViewById(R.id.password);
// Retriving value
if (savedInstanceState != null) {
username.setText(savedInstanceState.getString("USER_NAME"));
password.setText(savedInstanceState.getString("PASSWORD"));
}
return view;
}
2) Activity State::
Create a new Instance when the activity launches for the first time
else find the old fragment using a TAG and the FragmentManager
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
fragmentManager = getSupportFragmentManager();
if(savedInstanceState==null) {
userFragment = UserNameFragment.newInstance();
fragmentManager.beginTransaction().add(R.id.profile, userFragment, "TAG").commit();
}
else {
userFragment = fragmentManager.findFragmentByTag("TAG");
}
}
You can see the the full working code HERE
Below code is work for me. Need to care two things.
Each Input Field (Edit Text or TextInputEditText) assign unique id.
Manifest activity declaration should have on configuration change attribute with below values.
android:configChanges="orientation|keyboardHidden|screenSize"
Sample activity declaration in manifest.
<activity
android:name=".screens.register.RegisterActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:exported="true"
android:label="Registration"
android:theme="#style/AppTheme.NoActionBar" />
Sample declaration of
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/inputLayout"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:boxCornerRadiusBottomEnd="#dimen/boxCornerRadiusDP"
app:boxCornerRadiusBottomStart="#dimen/boxCornerRadiusDP"
app:boxCornerRadiusTopEnd="#dimen/boxCornerRadiusDP"
app:boxCornerRadiusTopStart="#dimen/boxCornerRadiusDP">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/inputEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:fontFamily="#font/proxima_nova_semi_bold"
android:inputType="textCapWords"
android:lines="1"
android:textColor="#color/colorInputText"
android:textColorHint="#color/colorInputText" />
</com.google.android.material.textfield.TextInputLayout>
this may help you
if your android:targetSdkVersion="12" or less
android:configChanges="orientation|keyboardHidden">
if your android:targetSdkVersion="13" or more
android:configChanges="orientation|keyboardHidden|screenSize">

Regain focus to views in dialog after orientation changes

Im using One Gulli Bulls Screen Orientation Change – Saving Focus example to regain focus when orientation changes.
My activity AddGearbox and opens a dialog CustomizeDialog (activity with theme.dialog).
The dialog has some textboxes. In onSaveInstanceState i save what I need and am able to restore it in restoreDialog. But the focus is not restored.
I am logging the id of the view that has focus when orientation changes but I guess that view is in the activity showing the dialog and not in the dialog itself?
What to do?
AddGearbox.java
public class AddGearbox extends FragmentActivity implements
MyListFragment.MyOnClickSelectedListener, DialogInterface.OnDismissListener {
CustomizeDialog customizeDialog;
#Override
public void onCreate(Bundle savedInstanceState) {
customizeDialog = new CustomizeDialog(this);
if(savedInstanceState!=null) {
restoreDialog(savedInstanceState);
}
}
....
....
....
#Override
protected void onSaveInstanceState(Bundle saveState) {
super.onSaveInstanceState(saveState);
saveState.putBoolean("isDialogOpen",isDialogOpen);
if (isDialogOpen) {
// Save a lot of stuff
View focusedChild = getCurrentFocus();
if (focusedChild != null)
{
int focusID = focusedChild.getId();
int cursorLoc = 0;
if (focusedChild instanceof EditText)
{
cursorLoc = ((EditText) focusedChild).getSelectionStart();
}
saveState.putInt("focusID", focusID);
saveState.putInt("cursorLoc", cursorLoc);
}
}
}
private void restoreDialog(Bundle savedInstanceState) {
isDialogOpen=savedInstanceState.getBoolean("isDialogOpen");
if (isDialogOpen) {
customizeDialog = new CustomizeDialog(this);
// Restore a lot of stuff
int focusID = savedInstanceState.getInt("focusID", View.NO_ID);
View focusedChild = findViewById(focusID);
if (focusedChild != null) {
focusedChild.requestFocus();
if (focusedChild instanceof EditText) {
int cursorLoc = savedInstanceState.getInt("cursorLoc", 0);
((EditText) focusedChild).setSelection(cursorLoc);
}
}
customizeDialog.show();
}
CustomizeDialog.java
public class CustomizeDialog extends Dialog implements OnClickListener {
public CustomizeDialog(AddGearbox mainActivity) {
super(mainActivity);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.customizedialog);

Categories

Resources