Android Add Fragment Without Transaction - android

I'm trying to replace a Fragment with another Fragment dynamically in my activity.
It looks like you can't replace a fragment statically defined in a layout file, with a dynamically created fragment:
Android: can't replace one fragment with another
The suggested solution was to add the original Fragment dynamically in the onCreate method:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ShelfFragment shelves = new ShelfFragment();
ft.add(R.id.left_fragment, shelves);
ft.addToBackStack(null);
ft.commit();
}
This works, but when the user presses the back button, the original Fragment is removed instead of closing the Activity because the FragmentTransaction added it to the FragmentManager stack.
Is there a way to add the initial Fragment to my activity without a Transaction/Stack entry?

Don't add it the backstack. Delete the ft.addToBackStack(null); line, you only need this if you want to be able to go back to the previous state with the back button.

Related

how to restore last opened fragment inside an activity after getting back from another actvivty

I'm using multiple fragments inside an activity like this flow: Fragment A has a list on it's item click Fragment B opens and it also has a list opens Fragment C which has a list Open another Activity , The problem is when I go back from the other Activity I found Fragment A is opened, How I restore the last Fragment C when go back from the other activity?
here is the code of replacing Fragment inside my activity
protected void showFragment(Fragment fragment) {
String TAG = fragment.getClass().getSimpleName();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container_home, fragment, TAG);
fragmentTransaction.addToBackStack(TAG);
fragmentTransaction.commit();
}
replace removes the existing fragment and adds a new fragment. This means when you press back button the fragment that got replaced will be created with its onCreateView being invoked. Whereas add retains the existing fragments and adds a new fragment that means existing fragment will be active.
Use add instead of replace
fragmentTransaction.add(R.id.container_home, fragment, TAG);
I had the same issue solved using the above code.
You can use
fragmentTransaction.addToBackStack(null);
instead of,
fragmentTransaction.addToBackStack(TAG);
Hope Your problem will solve.

Activity Buttons still ACTIVE when replaced by Fragment

Basically, I want to open a fragment from Activity. Everything worked fine (activity button opens the fragment and the fragment shows), until I miss-clicked outside the fragment's layout where one of the Activity button is located and it did its onClick method. That's why I wondered why is my Activity still active.
this is how I open the fragment from Activity
I tried using .add instead of .replace still the same.
#OnClick(R.id.login_register_lbl)
public void registerAction(View view) {
// TODO register transition...
FragmentManager fragmentManager = (LoginActivity.this.getFragmentManager());
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
RegisterFragment fragment = new RegisterFragment();
fragmentTransaction.replace(R.id.fragment_register_holder, fragment);
fragmentTransaction.commit();
}
In fragment layout in root element set "background" , "clickable=true" & Focusable="true".
In my opinion you can achieve this functionality by 2 ways:
Open a dialog fragment in full screen mode to avoid accidentally clicks on activities views.
If you wanted to show both fragment's and activity's views at the same time then, on click of your activity's button which opens the fragment, disable it by yourbutton.setenabled(false); and when you wanted to close the fragment enable that button again.

Android toolbar showing images of previously added fragment

I used toolbars in fragments instead of inflating it to activity.I am using different ImageViews in each toolbar layout.I was working fine. When I changed fragment transaction replace by add it is showing images of current layout images and previous fragment's images also.transaction.add(R.id.fragment_container, newFragment);What can I do?
I replaced replace by add since it showing some transparency while animating the second fragment from bottom.
This is how I added second fragment,
Fragment newFragment = new DetailsFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.slide_in_up, R.anim.splashfadeout);
transaction.add(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
if You use image different image in different fragment then you must hide or remove resourse onDestroy like it.
#Override
public void onDestroy() {
super.onDestroy();
MainActivity activity = (MainActivity) getActivity();
activity.mToolbarRightImageView.setVisibility(View.GONE);
}
if in which fragment you not use that ToolBarimage then on create of fragment you also hide that image.I faced this problem but i solved this way
MainActivity activity = (MainActivity) getActivity();
activity.mToolbarRightImageView.setVisibility(View.GONE);
If You want to use toolBar ImageView in each fragment , You make public in Mainactivty then you use above instruction

FragmentTransaction add() behavior

Working with fragments I've always used replace() for my transactions, but I wish I didn't have to save instance states anymore to restore a fragment's view and prevent reloading when coming back to that fragment. So, I've decided to work with add(). The thing is when I add another fragment, the previous fragment view remains in the background and that's fine (that's the behavior I expected), but the problem is I can actually interact with the views in the background. Example:
Fragment A has a Button
Fragment B has a TextView
When I add Fragment A and later add Fragment B, I'm able to click on Fragment A's Button, even staying on Fragment B's view.
I'm using:
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction().
add(getRootViewContainer(),fragment,fragment.getClass().getSimpleName());
if (shouldGoBack)
fragmentTransaction.addToBackStack(fragment.getClass().getSimpleName());
where getRootViewContainer() returns the id of the FrameLayout I'm using as my activity main container.
Now, is it really the default behavior of add()?
If so, is there a proper way to avoid this or one just has to use replace()?
What you can do here is just hide previous fragment at the time of transaction of current fragment.
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment newFragment= new MyFragment ();
ft.hide(CurrentFragment.this);
ft.show(newFragment);
ft.commit();
It worked for me just try it.
FragmentTransaction.hide(fragmentBehind); //works for me!
example :
//I have it globally available
FragmentTransaction trans = MainActivity.getManager().beginTransaction();
//not globally
FragmentTransaction trans = getFragmentManager().beginTransaction();
MapFragment newFragment = new newFragment();
trans.add(R.id.fragmentContainer, newFragment, tag);
trans.hide(this);
trans.addToBackStack(tag);
trans.commit();
Yes, this is a default behaviour of add().
If you really don't want to user replace(), you can try to disable views which are inside "old" fragment.

back stack and statically added fragment

I have a fragment added statically from XML I want to replace this fragment by another fragment, I did that by adding this code:
CFragment singleStationFragment = new CFragment();
android.support.v4.app.FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.layoutlist, singleStationFragment);
transaction.addToBackStack(null);
transaction.commit();
the problem is that when I press the back button the first fragment is not shown because it was not added through a transaction and the manager doesn't know about it, is there a way I could add the first fragment (ALREADY ADDED FROM XML), to my backstack or I could just show it when I click back instead of exiting the app ? Thanks !
As far as I'm aware you will have to add your first fragment to the activity in code rather than in the layout file. Do this with the add method of FragmentTransaction
transaction.add(R.id.FragmentContainer, fragment);

Categories

Resources