Open bottom sheet dialog only for one time - android

I have an issue with my Bottom Sheet Dialog. How do make my bottom sheet dialog only open for one time? If I clicked the button twice from my activity, it will open up the dialog twice. How to check whether the dialog is open or not from the activity? If the dialog is open, it will dismiss if I click the button. Here's my code to open the fragment from the activity. Thanks
Bundle bundle = new Bundle();
bundle.putString("outletData", new Gson().toJson(outletData));
BookOutletDialogFragment bookOutletDialogFragment = BookOutletDialogFragment.newInstance();
bookOutletDialogFragment.setArguments(bundle);
bookOutletDialogFragment.show(getSupportFragmentManager(), bookOutletDialogFragment.TAG);

set the flag in your Activity as:-
public static boolean isFragmentShown=false;
modify your onPause() and onStart() method of your fragments as:-
#Override
public void onStart() {
super.onStart();
Log.e( "Fragment is visible", "Fragment is visible");
**YouActivityName**.isFragmentShown = true;
}
#Override
public void onPause() {
super.onPause();
Log.e("Fragment is not visible", "Fragment is not visible");
**YouActivityName**.isFragmentShown = false;
}
And in your click method add this code in your top to check fragment is visible or not:-
if(!isFragmentShown){
Bundle bundle = new Bundle();
bundle.putString("outletData", new Gson().toJson(outletData));
BookOutletDialogFragment bookOutletDialogFragment = BookOutletDialogFragment.newInstance();
bookOutletDialogFragment.setArguments(bundle);
bookOutletDialogFragment.show(getSupportFragmentManager(),
bookOutletDialogFragment.TAG);}

While Afzal Khan's answer is true in my case onPause() method never called after doing something (ex: Pressing button) inside Bottom Sheet Dialog. So I just overrided onDismiss() method as I observed that onDismiss() method is called everytime when you dismiss dialog unlike onPause()
#Override
public void onDismiss(DialogInterface dialog) {
System.out.println("onDismiss() method called");
**YouActivityName**.isFragmentShown = false;
}

Related

How to Re Active onClick Listener in onResume Activity

I have created an Activity named BaseNavigationActivity that createsNavigationDrawer. All other activities are extending from this activity. It works fine,open the drawer on button click.
But when i go into another activity, and come back to previous Activity. The button stops working. Although visually button is still there, and if i click and drag from left side, it opens the Drawer Layout
When i debug it, i found out that it calls onRestart() and onResume() methods, but there is no code written in these method.
So do i need to add the code there as well? or is there any way to implement onClickListner in onRestart() and onResume() of Activity
onCreate()
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout_main);
headerLayout= (FrameLayout) findViewById(R.id.header_layout);
btnOpenDrawer= (Button) findViewById(R.id.btn_openDrawer);
homeLayout= (LinearLayout) findViewById(R.id.home_layout);
btnOpenDrawer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
drawerLayout.openDrawer(Gravity.LEFT);
}
});
#Override
protected void onResume() {
super.onResume();
}
#Override
protected void onRestart() {
super.onRestart();
}
Create class level ClickListener like onClickListener and Use it wherever you want.
// Activate Listener
myView.setOnClickListener(onClickListener);
// DeActivate Listener
myView.setOnClickListener(null);
// Listener
View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
}
};
In my opinion the error is that you set your OnClickListener in the onCreate Method. The OnClickListener only gets set when you create the activity. When you switch to your second activity, the OnClickListener will get overwritten and when you go back to your first activity the OnClickListener will not be set, because the first activity was already created, and only onResume will get called.
Try to move your code from the onCreate to onResume, so the listener will get set every time the activity is moving back to the front.

what is correct approach to perform UI related task when a dialog gets dismissed

I have a dialog with onDismiss handler:
public class TextReaderDialog extends DialogFragment {
...
public void onDismiss() {
}
I show this dialog and add some styles to a part of text from the fragment:
TextReaderDialog d = new TextReaderDialog();
d.show(getFragmentManager(), "sample");
Spannable spannableText = new SpannableString(tv.getText());
spannableText.setSpan(new BackgroundColorSpan(Color.LTGRAY), startOffset, startOffset + w.word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(spannableText);
Whenever a dialog is dismissed, I want to remove styles from the text. How can I do that? What is the correct way to do that?
The simplest way to go about this would be to add a method to your fragment like so:
public void dismissStyles(){
//do your style dismissing here
}
Now, I assume in the dialog you are overriding DialogFragment.onDismiss(DialogInterface dialog). As long as that is the case, once you have completed that method, in your dialog's onDismiss function, you can do something to the effect of:
#Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
MyFragment fragment = (MyFragment) activity
.getFragmentManager()
.findFragmentByID(R.id.containerOfYourFragment);
if(fragment != null){
fragment.dismissStyles();
}
}
Here, activity should be the current activity that your fragment and dialog are hosted in. You can pass this to the dialog in a constructor, or depending on where the dialog is located. You could also just pass the current fragment to the dialog in the constructor as well, and then it would simply be called by myFragment.dismissStyles();.

After screen orientation change the dialogFragment appears apparently without any call

here there is part of the Activity where the screen orientation change:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.editText1);
et.setOnLongClickListener(new View.OnLongClickListener()
{
#Override
public boolean onLongClick(View v)
{
Fragment1 dialogFragment = new Fragment1();
dialogFragment.show(getFragmentManager(), null);
dialogFragment.setTextDialog(et.getText().toString());
return true;
}
});
}
Apparentely it seems that the dialog that will appear inside the DialogFragment should appear just after the onLongClick over the editText
(I know that when the screen orientation change the Activity is restarted, but it shouldn't start normally like the first time that is created?)
My problem:
when I open at least once the dialog and I close it, after the screen orientation change I have the dialog displayed again on the screen, like if I long-Clicked the editText.
I don't absolutely know why this happens.
I attach also the structure of dialog fragment:
public Dialog onCreateDialog(Bundle savedInstanceState)
{
final Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutInflater adbInflater = LayoutInflater.from(getActivity());
View eulaLayout = adbInflater.inflate(R.layout.dialog_crypt, null);
Button btn_OK = (Button) eulaLayout.findViewById(R.id.btnOK);
dialog.setContentView(eulaLayout);
final EditText et = (EditText)eulaLayout.findViewById(R.id.editText2);
et.setText(textDialog);
if(et.length()>0)
{
et.setText(et.getText().toString() + " ");
}
et.setSelection(et.length());
btn_OK.setOnClickListener(
new View.OnClickListener()
{
#Override
public void onClick(View v)
{
textDialog = et.getText().toString();
((Main)getActivity()).setTextOnEditText(textDialog);
dialog.dismiss();
}
});
return dialog;
}
Thanks so much for the help.
Try removing the dialog from stack using fragment manager instead of just dismissing it.
getFragmentManager().beginTransaction().remove(dialogFragment.this).commit();
By the way, instead of just using a Fragment for your dialog, you should use DialogFragment itself. Checkout: DialogFragment
Also, don't ever call your activity methods like this ( ((Main)getActivity()).setTextOnEditText(textDialog);
unless your fragment is a static inner class. Instead, create an interface to talk between fragments and activity.
When screen changes orientation, it calls onSaveInstanceState method, and it saves the state in the Bundle object including the stack. If you dismiss the dialog without clearing this stack, it will then show the dialog when you rotate the phone since this is in the saveInstanceState bundle.
You must clear dialog off the stack with:
getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit();
if you use support library for dialog fragment, or
getActivity().getFragmentManager().beginTransaction().remove(this).commit();
When a config change (like rotation) occurs the old Fragment isn't destroyed - it just adds itself back to the Activity when it's recreated (android retains fragments by default). So if you have your DialogFragment shown before rotation, it will instantly show up after rotation.

Dismiss popup window from Fragment like onBackPressed

I would like to dismiss a popup window when the user presses the back button. Since i am in the context of a Fragment i don't have the method onBackPressed() available.
Dismissing the popup shouldn't be something difficult as i just need to call the dismiss() method. Problem is i don't know how to detect the pressing of the back button
Could i use something similar for a Fragment or is there any other way i can detect the pressing of the Back button from this Fragment?
Thanks!
LATER EDIT => WHAT I TRIED TO DO
In my main activity i implemented the onBackPressed() method like this:
#Override
public void onBackPressed() {
//isThePopupShowing() is a method in the target fragment which returns true if the PopupWindow is currently showing
if (secondFragment.isThePoupShowing()) {
// dismissPopup is a method in the same fragment which closes the PopupWindow with the dismiss() method
secondFragment.dismissPopup();
Log.d("DismissPopup", "And finally here!");
} else {
super.onBackPressed();
}
}
when i create the fragment here:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.home);
SharedPreferences user_details = getSharedPreferences(
ro.gebs.captoom.utils.Constants.PREFS_NAME, 0);
LoginFragment firstFragment = new LoginFragment();
secondFragment = new HomeScreenFragment();
String userid = user_details.getString("userid", null);
manager = getSupportFragmentManager();
if (userid == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment).commit();
} else {
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, secondFragment).commit();
}
}
This is the code in my fragment:
public boolean isThePoupShowing() {
return sync_popup != null && sync_popup.isShowing();
}
//
public void dismissPopup() {
Log.d("DismissPopup", "I got here, dismissing");
sync_popup.dismissPopup();
}
And this is the dismiss method:
public void dismissPopup(){
layout.setVisibility(View.GONE);
dismiss();
Log.d("DismissPopup", "and in SyncQuickAction");
}
The back button works normally as the application closes once i press the back button when the fragment is open but the popup is not dismissed when i press the back button... any suggestions as to what i might be doing wrong?
Thanks
Replace
popupWindow.setOutsideTouchable(false);
with this
popupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(true);
The fragment is paused (lifecycle of fragment) when the popup window is showing, so sync_popup gets null value and isThePoupShowing method always gets false value when it's called from the activity.
When you make the popupWindow as a static member, the system will not "recycle" the member when the fragment is paused and you can dismiss it correctly.

what does onBackPress call in a Fragment

In a Fragment i have this:
onPause(){
super.onPause();
if(flag){
getActivity.finish();
}else{
}
}
onResume(){
flag = true;
super.onResume();
}
and there is a Button, on click i set this flag to false:
Button.onClick{
flag = false;
}
The idea is:
when the button is clicked don't finish this Activity. But when device BackButton is pressed finish().
But this logic isn't working. The reason is, when i press BackButton of the device, onPause is not being called.
This is the 4th Fragment in the Activty. So when i press BackButton i can see the 3rd Fragment. Which i don't want to.
I am using this on a API 10 device, But my application uses support library v4.
Thank You.
If you want to close the activity on the back press, then Override the onBackPressed inside the activity as following:
public void onBackPressed() {
Fragment fragment = getSupportFragmentManager().findFragmentByTag("yourfragment");
if (fragment instanceOf YourFragmet) {
finish();
return;
}
super.onBackPressed();
}
If you want to close the activity on the button click, then you can call
Button.onClick{
getActivity().onBackPressed();
}
OR
Button.onClick{
getActivity().finish();
}
If When you are transitioning between Fragments, you call addToBackStack() as part of your FragmentTransaction, then the back button will take you to the fragment on the top of the back stack
Why can't you use onDetach() of Fragment? On Back key pressed, there will be onDetach() called on the fragment to remove that.
#Override
public void onDetach() {
super.onDetach();
if(flag){
getActivity.finish();
}else{
}
}

Categories

Resources