I'm developing an application which consists of Fragments, When I click on the button it should show and hide. By default I have set fragments as visibility gone, even though this is also not working for me:
FragmentTransaction mFragmentTransaction = getFragmentManager().beginTransaction();
mMenuFragment = new MenuFragment();
mFragmentTransaction.add(R.id.frmMenuBar, mMenuFragment);
mFragmentTransaction.hide(mMenuFragment);
mFragmentTransaction.addToBackStack(null);
mFragmentTransaction.commit();
The code below I kept on button click.
#Override
protected void onMenuButtonClick(View Button) {
super.onMenuButtonClick(Button);
System.out.println("Botton Clciked");
if (Button.isClickable()) {
FragmentTransaction menuTransaction = getFragmentManager().beginTransaction();
menuTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
menuTransaction.show(mMenuFragment);
menuTransaction.addToBackStack(null);
menuTransaction.commit();
} else {
FragmentTransaction menuShowTransaction = getFragmentManager().beginTransaction();
menuShowTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
menuShowTransaction.hide(mMenuFragment);
menuShowTransaction.addToBackStack(null);
menuShowTransaction.commit();
}
}
Please can any one help me.
Related
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();
}
I have 2 fragment on one fragment activity. I have 2 fragments say new and pending but I am having problem while switching from one fragment to another while tapping of button. When I am on pending tab and then click on new tab then sometimes it shows two loader and then it still shows pending page. My both fragments are android.support.v4.app.Fragment. I am attaching screenshot of the problem while I am tapping on new tab and still showing pending tab with two loader.
code for switching between fragment
#Override
public void onClick(View v) {
if (v.equals(neww))
{
Fragment newpage = new NewPageActivity();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.framelay, newpage);
ft.commit();
}
else if(v.equals(pending))
{
Fragment pendingFragment = new PendingPage();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.framelay, pendingFragment);
ft.commit();
}
}
now screenshot of problem
Try this:
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.framelay, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
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)
I have been trying to build an android app for the purpose of understanding basic concepts behind fragments. But I am completely unable to show and hide fragment. Here is my code for onClickListner
final FragmentManager fragmentManager = getFragmentManager();
b.setOnClickListener(new View.OnClickListener() {
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void onClick(View v) {
FragmentTransaction ft = fragmentManager.beginTransaction();
PM_Fragment pm_fragment = new PM_Fragment();
ft.replace(android.R.id.content, pm_fragment);
if (pm_fragment.isHidden()) {
fragmentManager.beginTransaction()
.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
.show(pm_fragment)
.commit();
b.setText("Hide");
} else {
fragmentManager.beginTransaction()
.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
.hide(pm_fragment)
.commit();
b.setText("Show");
}
}
});
Fragment is simply a text line in my case. And what the button is supposed to do is toggle the visibility of the fragment.
Can anybody tell me what is wrong with this code?
And by not working, I mean that button does nothing when tapped, except for changing its text from "hide" to "show" and after that it keeps "show", no matter how many times you tap it. And this process has no effect on the behaviour of fragment at all.
I really don't understand what you are trying to do here, but you never commit ft so your Fragment is never added to the Activity. I also don't understand the purpose of the two inner FragmentTransaction, but it is save to say that you DO NOT need them at all...
Define this globally:
private PM_Fragment pmFragment = new PM_Fragment();
And your OnClickListener should look like this:
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
if(pmFragment.isAdded()) {
ft.remove(pmFragment);
} else {
ft.replace(android.R.id.content, pmFragment);
}
ft.commit();
}
});
IMPORTANT: For FragmentTransactions to work, the Fragment has to have been added in code! If you add them in XML then they cannot be affected by FragmentTransactions! So if added your Fragment like this:
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="at.example.fragments.SomeFragment" />
Or with some other similar method than I am sure that this is at least part of the problem. You need to add your Fragment solely in code like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
SomeFragment fragment = SomeFragment.newInstance();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.flFragmentContainer, fragment);
transaction.commit();
}
}
Just replace the <fragment /> tag with something like this in the layout:
<FrameLayout
android:id="#+id/flFragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
It will serve as a container for the Fragment you want to add. You can use the FragmentTransaction above to add the Fragment to this FrameLayout.
After struggling to make my simple fragment program work, I have not found any solution to the following: I manage two Fragments in my main activity: FragmentNeedle and FragmentPlot. Only one should appear at a time. The user has two bottons where he can select which fragment he wants to display. Also, when the FragmentPlot is showing, the user should be able to navigate back to the FragmentNeedle by pressing the back key. This is my Code:
public void onButtonPlotPressed()
{
FragmentManager manager = getSupportFragmentManager();
mPlotFragment = (PlotFragment) manager.findFragmentByTag(PlotFragment.class.getSimpleName());
if(mPlotFragment == null)
{
mPlotFragment = new PlotFragment();
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.main_layout_center, mPlotFragment, PlotFragment.class.getSimpleName());
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
manager.executePendingTransactions();
}
public void OnButtonNeedlePressed()
{
FragmentManager manager = getSupportFragmentManager();
mFragmentNeedle = (FragmentNeedle) manager.findFragmentByTag(FragmentNeedle.class.getSimpleName());
FragmentTransaction ft = manager.beginTransaction();
if(mFragmentNeedle == null)
{
mFragmentNeedle= new FragmentNeedle();
ft.replace(R.id.main_layout_center, mFragmentNeedle, FragmentNeedle.class.getSimpleName());
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
manager.executePendingTransactions();
}
}
When the PlotFragment is shown, and I press back, I return to the NeedleFragment. But now when I try to change to Plot Fragment by pressing the UI button, it will keep showing the PlotFragment. If I remove the line ft.addToBackStack(), the switching between fragments works fine by pressing the buttons on the UI, but then I cannot go back with the back key. What am I doing something wrong?
you never add your FragmentNeedle to the backstack.
public void OnButtonNeedlePressed() {
mFragmentNeedle = (FragmentNeedle)manager.findFragmentByTag(FragmentNeedle.class.getSimpleName());
if(mFragmentNeedle == null) {
FragmentManager manager = getSupportFragmentManager();
mFragmentNeedle= new FragmentNeedle();
ft.replace(R.id.main_layout_center, mFragmentNeedle, FragmentNeedle.class.getSimpleName());
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
//missing line
ft.addToBackStack(FragmentNeedle.class.getSimpleName());
ft.commit();
manager.executePendingTransactions();
}
}