Hide/Show floating action button during FragmentTransection - android

I have this code for open some Message Fragment.
private void openMessage(int position) {
MessageRecord item = data.get(position);
item.setIsRead(true);
item.save();
List<MessageRecord> tmp = new ArrayList<>(data);
updateScreen(tmp);
MessagesActivity2 parentActivity = ((MessagesActivity2) context);
parentActivity.fab.hide();
FragmentTransaction ft = parentActivity.getSupportFragmentManager().beginTransaction();
ft.addToBackStack(null);
MessageFragment fragment1 = MessageFragment.newInstance(item);
ft.add(R.id.frame, fragment1);
ft.commit();
}
And this line hides my fab.
parentActivity.fab.hide();
But I can't figure out how to reopen my FAB after I press Back button and return to the view of the activity.

Override onBackPressed() in the activity and inside check if the fragment is null or not as per your requirement, then hide your FAB.

Instead of adding FAB on your Activity directly,add FAB on a fragment(FABFragment) and add another fragment(MessageFragment) for your message.
So whenever you want to show MessageFragment replace the FABFragment using following code:
MessageFragment fragment1 = MessageFragment.newInstance(item);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.addToBackStack(null);
ft.replace(R.id.frame, fragment1);
To communicate between your fragments use interfaces(https://developer.android.com/training/basics/fragments/communicating.html)

Related

Fragment does not close

I'm creating a fragment which is supposed to act like a menu. I have successfully inflated it to the activity where I wanted it to be, however I now I realise that I cannot close the fragment. Furthermore I am able to scroll the contents of the activity which the fragment is placed over. How can I edit my code in such a way that the fragment will close after an action on the activity is detected, or one of it's contents is clicked?
I created the fragment by simply adding a fragment via new -> Fragment -> Fragment(Blank). I have not touched any of the code and have initialized the fragment like so in a on click:
findViewById(R.id.Menu).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.enter_from_left, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_left);
MenuFragment menuFragment = new MenuFragment();
fragmentTransaction.replace(android.R.id.content, menuFragment);
fragmentTransaction.commit();
}
});
This is what it looks like, ignore the horrendous design.

how to remove fragment over an activity layout

I am really new to android development. I have created a simple main activity and add an icon on top left. Clicking it I can show a blank fragment.on my screen replacing a layout which is loaded in onCreate method. Now clicking another icon I want to hide that fragment and load that layout again. How to do that?? any helps??
Below is my code
//part of oncreate where my layout is loaded
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// part of code when icon clicked and fragment is loaded
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
BlankFragment frag = new BlankFragment();
fragmentTransaction.replace(R.id.content_main, frag);
fragmentTransaction.commit();
//another nearby icon clicked
//now i want to replace this fragment from content_main layout
//what code to add??
If I got your question right, then this is the right answer I guess.
//keep track of all fragments you add by tagging
fragmentTransacaction.add(R.id.content, new FragA(), "first");
//and when removeing
Fragment f = getFragmentManager().findFragmentByTag("first");
if(f!=null) fragmentTransac.remove(f);
fragmentTransac.commit();
I got this from here
You can toggle visibilty by below code.
public void toggleVisibility(Fragment fragment){
FragmentTransaction transaction = getFragmentManager().beginTransaction();
if (fragment.isHidden()) {
transaction.show(fragment);
} else {
transaction.hide(fragment);
}
transaction.commit();
}

how to switch from one fragment to another fragment android

Hi I have 5 fragments Now first time when I go to Activity of that fragments then default First fragment is called,then on first Fragment there is a button by clicking that button I go to the second fragment,similarly in Second fragment There is a button and clicking that button I go to the third fragment and so on.
Now My Question is that Current I am on Fifth fragment ,Now I want to go fifth fragment to second fragment,what should I do for this?
Can any one please tell me?
You can pop the fragment by name. While adding fragments to the back stack, just give them a name.
fragmentTransaction.addToBackStack("frag2");
Then in fragment5, pop the back stack using the name ie.. frag2 and include POP_BACK_STACK_INCLUSIVE
someButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fm = getActivity()
.getSupportFragmentManager();
fm.popBackStack ("frag2", FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
});
Here is the method to add and replace fragment.
public void addReplaceFragment(Fragment fragment, int addOrReplace) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
switch (addOrReplace) {
case addFragment:
transaction.add(R.id.frame_containerforsearchable, fragment);
transaction.commit();
break;
case replaceFragment:
transaction.replace(R.id.frame_containerforsearchable, fragment);
transaction.commit();
break;
default:
break;
}
}
call of fragment is..
addReplaceFragment(currencyFragmentWithSearch, replaceFragment);
replaceFragment integer variable
currencyFragmentWithSearch Fragment instance
You can always do this :)
getActivity().getSupportFragmentManager().replace(R.id.yourFrameLayout,new Fragment2(),"FRAG2");
OR
Fragment fragment = getActivity().getSupportFragmentManager().findFragmentByTag("FRAG2");
getActivity().getSupportFragmentManager().replace(R.id.yourFrameLayout,,"FRAG2");
Try this code:
Fragment fragment = new SecondFragmentName();
FragmentManager fm = getActivity().getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
just small correction to mudit_sen code. It is working, only if you add .beginTransaction. Thank you.
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.FrameLayoutId,new FragmentName()).commit();

Opening Fragment from a DialogFragment (replacing the Dialogs parent)

Let's say I have Fragment A, from which I open a DialogFragment like this:
FragmentActivity fragmentActivity = (FragmentActivity) view.getContext();
FragmentTransaction ft = fragmentActivity.getSupportFragmentManager().beginTransaction();
Fragment prev = fragmentActivity.getSupportFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
DialogFragment fragmentDialog = MyDialogFragment.newInstance();
fragmentDialog.show(ft, "dialog");
From this Dialog, after clicking (positive / neutral / negative) button, I want to open Fragment B, which should replace Fragment A.
In the Dialog's onClick method I run a callback method of parent Activity:
#Override
public void onClick(DialogInterface dialog, int which) {
switch(which) {
case DialogInterface.BUTTON_NEUTRAL:
detailsCallbacks.openMoreDetails();
break;
}
}
And finally my Activity's openMoreDetails() method looks like this:
#Override
public void openMoreDetails() {
Fragment fragmentB = Fragment.newInstance();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container, fragmentB);
ft.addToBackStack(null);
ft.commit();
}
What I get is strange. Fragment B blinks on the screen just for a fraction of a second and then is replaced (covered?) by Fragment A again.
When I click the 'up' button I get back from Fragment A, so none of these transactions were added to the back stack. I would like to show Fragment B and then, when pressing the 'up' button, go back to Fragment A.
Is it somehow possible? And what's wrong with my approach?
Just had the same problem:
Fragment A display a custom dialog fragment.
At click on one of the buttons of the dialog fragment, I wanted to remove the dialog and show Fragment B.
Fragment B was displayed and instantly disappear. My screen was displaying Fragment A again.
What was wrong on my initial implementation:
private void onClickInscription() {
FragmentInscription frag = FragmentInscription.newInstance();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.main, frag);
ft.addToBackStack(null);
ft.commit();
dismiss();
}
And the correct one:
private void onClickInscription() {
dismiss();
FragmentInscription frag = FragmentInscription.newInstance();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.main, frag);
ft.addToBackStack(null);
ft.commit();
}
So try to call dismiss first on your dialog then apply the FragmentTransction
I know this it's been a long time since the problem was posted, but I solved it by adding a if(fragmentB.getView != null) before the replace statement.
It finally doesn't do the flash and disappear thing. (:
You shouldn't be opening the dialog from FragmentA. Implement a callback from your FragmentA to your activity and let the activity handle all fragment transactions.

Switch between two fragments

I want to do the following. There are two fragments first and second. Necessary make the transition between them. When I go from first fragment in the second, first stored in the stack. When I click the Back button the second fragment is removed and returned first fragment from the stack. Again I can not go in the second fragment - it has been deleted. How can I solve this problem?
In main activity (callback for Fragment1):
#Override
public void onNavigate() {
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment1 newFragment1 = (Fragment1) getFragmentManager().findFragmentByTag("frag_1");
Fragment2 newFragment2 = (Fragment2) getFragmentManager().findFragmentByTag("frag_2");
ft.replace(R.id.main, newFragment2);
ft.remove(newFragment1);
ft.addToBackStack(null);
ft.commit();
}
Fragments I added dynamically:
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.main, new Fragment1(), "frag_1");
ft.add(R.id.main, new Fragment2(), "frag_2");
ft.commit();
I solved this problem :). I hide first fragment and add transaction to the back stack. When I click button Back I return to fragment
#Override
public void onNavigate() {
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment1 newFragment1 = (Fragment1) getFragmentManager().findFragmentByTag("frag_1");
ft.hide(newFragment1);
ft.addToBackStack(null);
ft.commit();
}

Categories

Resources