I want to handle onBackPressed in fragment
I use following code in my activity but this return 0
#Override
public void onBackPressed() {
int count = getFragmentManager().getBackStackEntryCount();
Toast.makeText(getBaseContext(), ""+count, Toast.LENGTH_SHORT).show();
if (count == 1) {
super.onBackPressed();
//additional code
} else {
getFragmentManager().popBackStack();
}
}
Just add .addToBackStack(null) in your Activity or Fragment . When you adding or replacing fragment. Like below
getSupportFragmentManager().beginTransaction().replace(R.id.parent_layout, new MyFragment()).addToBackStack(null).commit();
Note:- you don't have to do anything in your onBackPressed() method.
Just app this line when you want to replace any fragment and manage backsack on Fragment.
ClientHome per = new ClientHome();
Bundle bundle = new Bundle();
bundle.putString("usertype", "client");
per.setArguments(bundle);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.slide_from_left, R.anim.slide_to_right);
fragmentTransaction.replace(R.id.content_frame, per, "tag");
fragmentTransaction.addToBackStack("tag");
fragmentTransaction.commit();
Related
I have an issue with onBackPressed() implementation in my app. I have a single fragment with differents tags. The user could navigate between fragments with a Navigation View and the back button. Everything works as expected except when FragmentTransaction goes like this:
Initial Fragment->A->B->C->A
When I use back button to return, the behavior I want to achieve is A->C->B->Initial.
Instead, I get A->B->C->A->Initial
How can I acomplish my desired behavior?
This is what I have so far:
private int backStackEntries = 0;
public void onBackPressed() {
Log.d(TAG,"Number of fragments in back: "+backStackEntries);
NewsListFragment fragment = (NewsListFragment) getSupportFragmentManager().findFragmentByTag(currentFrag);
backStackEntries-=1;
if(backStackEntries<0)
backStackEntries = 0;
if(fragment.getTag().equals(News_TAG[0])){
if(fragment.getParserMaker().isRunning()){
moveTaskToBack(true);
}
else{
finish();
}
}
else{
currentFrag = getSupportFragmentManager().getBackStackEntryAt(backStackEntries).getName();
getSupportFragmentManager().beginTransaction()
.hide(fragment)
.show(getSupportFragmentManager().findFragmentByTag(currentFrag))
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit();
}
}
private void makeFragmentTransaction(String[] urls, int item,String _TAG) {
Bundle bundle = new Bundle();
bundle.putStringArray("urls", urls);
NewsListFragment newsFragment = (NewsListFragment) getSupportFragmentManager().findFragmentByTag(_TAG);
if(newsFragment == null){
newsFragment = new NewsListFragment();
}
newsFragment.setArguments(bundle);
if(currentFrag == null){
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, newsFragment, _TAG)
.addToBackStack(_TAG)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit();
currentFrag = _TAG;
}
else if(!newsFragment.isAdded()){
getSupportFragmentManager().beginTransaction()
.hide(getSupportFragmentManager().findFragmentByTag(currentFrag))
.add(R.id.container,newsFragment,_TAG)
.addToBackStack(_TAG)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit();
currentFrag = _TAG;
backStackEntries+=1;
}
else if(!currentFrag.equals(_TAG)){
getSupportFragmentManager().beginTransaction()
.hide(getSupportFragmentManager().findFragmentByTag(currentFrag))
.show(newsFragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit();
backStackEntries+=1;
currentFrag = _TAG;
}
navigationView.setCheckedItem(item);
drawerLayout.closeDrawers();
}
Adding the last backStackEntries+=1; makes the weird behavior and erasing that line makes the deal BUT it creates another Transaction issue:
Initial(going back)->B->C
Pressing back button: C->Initial
I have also tried making transactions like this:
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, newsFragment, _TAG)
.addToBackStack(_TAG)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit();
currentFrag = _TAG;
And onBackPressed like this:
NewsListFragment fragment = (NewsListFragment) getSupportFragmentManager().findFragmentByTag(currentFrag);
if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
getSupportFragmentManager().popBackStack();
} else {
if (fragment.getParserMaker().isRunning()) {
moveTaskToBack(true);
} else {
finish();
}
}
This also cause the undesired behavior and popBackStack() removes fragment, which I don't want to do.
I also had the same problem, I solved it by implementing my own history stack for the fragments. everytime that I wanted to add a fragment to the stack, I would look through the stack and removed the fragment with the same class as the one I was gonna add (if there was one)
My app flow is:
Activity A -> Activity B (this activity has buttons which on click open fragments)
I want that when I am on Activity B's fragments, on back pressed the flow should be to Activity B and when I am on Activity B then on back pressed app should exit.
I am currently using:
#Override
public void onBackPressed() {
if(getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStackImmediate();
}
else {
super.onBackPressed();
}
}
It's working fine when back pressed on Activity B's fragments, but it's going to Activity A when pressed back from Activity B(and that too for some reason nothing is being displayed in Activity A).
EDIT
My code for calling fragment:
faqsBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mLoginSubmit.setVisibility(View.GONE);
Bundle b1 = new Bundle();
b1.putString("ComingFrom","Login");
android.support.v4.app.Fragment faqsFragment = new fragment11Faqs();
faqsFragment.setArguments(b1);
android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.framelayoutfaqs,faqsFragment);
transaction.addToBackStack(faqsFragment.toString());
transaction.commit();
}
});
You should call finish() when you call Activity B right after declaring its intent from ActivityA
Like this
Intent intent=new Intent(ActivityA.this,ActivityB.class);
startActivity(intent);
finish();
And you code for backstack is fine in activity b just add finish() after super.onBackPressed();
#Override
public void onBackPressed() {
if(getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStackImmediate();
}
else {
super.onBackPressed();
finish();
}
}
Here is how to add fragment to backstack
Fragment cameraEditing = new Editing(); FragmentManager
fragmentManager = getFragmentManager(); FragmentTransaction
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.cameraLayout,
cameraEditing,"cameraEditing");
fragmentTransaction.addToBackStack("cameraEditing");
fragmentTransaction.commit();
My application had a bottom navigation bar which has 5 tabs.
So according to these tabs, I have 5 fragments
When I click on the tab, the fragment changed according to that tab.
I can switch fragment by using the method beginTransaction().replace...
I dont want the fragment to be destroyed and recreated again each time I switch tabs, so my solution is sth like this
//I init 5 fragments
Fragment1 frag1 = new Fragment1();
Fragment2 frag2 = new Fragment2();
Fragment3 frag3 = new Fragment3();
Fragment4 frag4 = new Fragment4();
Fragment5 frag5 = new Fragment5();
//When I click on tab, for example tab1, I hide all fragments except tab1
//hide all fragments
getSupportFragmentManager()
.beginTransaction()
.hide(fragment1) //Fragment2, 3, 4, 5 as well
.commit();
//show fragment 1
getSupportFragmentManager()
.beginTransaction()
.show(fragment1)
.commit();
It works very well, but the problem is sometimes 2 fragments show at once time (I dont know why because I hide all fragments)
Any other way to achieve that? Switch fragment without destroying it and creating it again.
for adding fragment I make this code for my project, hope it will be help.
public static void replaceFragment(Fragment fragment, FragmentManager fragmentManager) {
String backStateName = fragment.getClass().getName();
String fragmentTag = backStateName;
Fragment currentFrag = fragmentManager.findFragmentById(R.id.frame_container);
Log.e("Current Fragment", "" + currentFrag);
// boolean fragmentPopped = fragmentManager.popBackStackImmediate(backStateName, 0);
int countFrag = fragmentManager.getBackStackEntryCount();
Log.e("Count", "" + countFrag);
if (currentFrag != null && currentFrag.getClass().getName().equalsIgnoreCase(fragment.getClass().getName())) {
return;
}
FragmentTransaction ft = fragmentManager.beginTransaction();
// if (!fragmentPopped) {
ft.replace(R.id.frame_container, fragment);
ft.addToBackStack(backStateName);
ft.commit();
// }
currentFrag = fragmentManager.findFragmentById(R.id.frame_container);
Log.e("Current Fragment", "" + currentFrag);
}
hope this will be help you, and use this method in entire project for replacing fragment.
Using ViewPager with FragmentPagerAdapter suits for you in this case.
Then use ViewPager#setOffsetPageLimit(5). This will help you show/hide your fragments without recreating it again.
Follow this tutorial
Let try it, then tell me if your problem is solved or not. ;)
you don't have to need to hide the fragment just replace the fragment like this method:
public void setFragment(Fragment fragmentWhichYouWantToShow) {
fm = getSupportFragmentManager();
ft = fm.beginTransaction();
ft.replace(R.id.container, fragmentWhichYouWantToShow);
ft.commit();
}
Try to make it in a singe transaction.
protected void showAsRootFragment(Fragment fragment, #NonNull String tag) {
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = supportFragmentManager.beginTransaction();
if (supportFragmentManager.getFragments() != null) {
for (Fragment attachedFragment : supportFragmentManager.getFragments()) {
if (attachedFragment != null && !tag.equals(attachedFragment.getTag())) {
transaction.hide(attachedFragment);
attachedFragment.setUserVisibleHint(false);
}
}
}
if (!fragment.isAdded()) {
transaction.add(R.id.frame_container, fragment, tag);
fragment.setUserVisibleHint(true);
} else {
transaction.show(fragment);
fragment.setUserVisibleHint(true);
}
transaction.commit();
}
I've been wracking my brain for the greater part of the weekend, trying to solve an issue related to Android Studio. The assignment is to create a list fragment that will update when the user clicks buttons in the overhead bar, but despite my best efforts only one of the fragments actually displays (and it's the same no matter which button the user clicks). I've narrowed it down to the point where I'm fairly certain the problem has to do with the constructor's handling of bundles, but I'm not sure where the error is. Here's the related code:
public static ItemFragment newInstance(String title, int position) {
ItemFragment fragment = new ItemFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1,title);
args.putInt(ARG_PARAM2,position);
fragment.setArguments(args);
return fragment;
}
And later on, the call to getArguments():
public void onCreate(Bundle savedInstanceState) {
setRetainInstance(true);
if (getArguments() != null) {
mTitle=getArguments().getString(ARG_PARAM1);
mPosition=getArguments().getInt(ARG_PARAM2);
getActivity().setTitle(mTitle);
}
if (mPosition == 0) {
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, android.R.id.text1, listItems));
} else if (mPosition == 1) {
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, android.R.id.text1, newGoalItems));
} else if (mPosition == 2) {
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, android.R.id.text1, markProgressItems));
} else if (mPosition == 3) {
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, android.R.id.text1, settingsItems));
}
}
This is the related code in MainActivity:
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_list) {
list=ItemFragment.newInstance("List", 0);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.add(R.id.list_frame, new ItemFragment());
fragmentTransaction.commit();
}
else if (id==R.id.action_newGoal) {
newGoal=ItemFragment.newInstance("New Goal", 1);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.add(R.id.list_frame, new ItemFragment());
fragmentTransaction.commit();
}
else if (id==R.id.action_markProgress) {
markProgress=ItemFragment.newInstance("Mark Progress", 2);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.add(R.id.list_frame, new ItemFragment());
fragmentTransaction.commit();
}
else if (id==R.id.action_settings) {
settings=ItemFragment.newInstance("Settings", 3);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.add(R.id.list_frame, new ItemFragment());
fragmentTransaction.commit();
}
return super.onOptionsItemSelected(item);
}
I know from testing that the code simply crashes through the if statement above. What I don't know is why... It looks to me like there should be some values in getArguments that prevent it from returning null.
Write this piece of code in fragments onCreate method() it will work
if (getArguments() != null) {
mTitle=getArguments().getString(ARG_PARAM1);
mPosition=getArguments().getInt(ARG_PARAM2);
getActivity().setTitle(mTitle);
}
It sounds like you are trying to read the arguments in your Fragment's constructor. The context will not be established yet in the constructor, so you should be reading from the bundle in the onCreate() method.
public static Fragment newInstance(String title, int position) {
Fragment fragment = new ItemFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1,title);
args.putInt(ARG_PARAM2,position);
fragment.setArguments(args);
return fragment;
}
Change this line ItemFragment fragment = new ItemFragment(); to this Fragment fragment = new ItemFragment(); and all your code is perfect.
I finally figured this out! So the issue was that during the process of loading the fragments into the frame (the third section of code), the professor gave us a line of code that instantiated a new fragment instead of using the one that we'd just created a few lines above. I simply replaced
fragmentTransaction.add(R.id.list_frame, new ItemFragment());
with
fragmentTransaction.add(R.id.list_frame, list);
and now everything runs. Thanks for all the help everyone!
I'm attempting to implement a back stack while using fragments, but when using the Back button, I keep getting taken out of the app to the home screen.
Activity opens fragment A; Fragment A has a clickable TextView that opens fragment B (this works). Hitting BACK should return me to fragment A, but it takes me to the home screen instead.
Here is the activity's call to the opening of fragment A in onCreate:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment_layout_container, new FragmentLogin(), "fragment_login")
.addToBackStack("login_screen")
.commit();
Log.d("Back", getFragmentManager().getBackStackEntryCount() +" <- Entry Count at LoginActivity.onCreate" );
At this point, the Log prints 0 <- Entry Count at LoginActivity.onCreate. Something I've done wrong keeps this from printing 1.
Then, the Fragment A has this listener:
forgottenPassword.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_layout_container, new FragmentForgottenPassword(), "fragment_password")
.addToBackStack("forgotten_password")
.commit();
Log.d("Back", getFragmentManager().getBackStackEntryCount() + " <- Entry Count at FragmentLogin.onCreateView.Listener");
}
});
The Log here prints 1 <- Entry Count at FragmentLogin.onCreateView.Listener. Here, the listener works and opens fragment B - but the back button returns me to the home screen.
Use this in your Activity it should pop out the fragments already added to backstack
#Override
public void onBackPressed()
{
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
Try Like This,
public void replaceFragment(Fragment fragment, boolean addToBackStack) {
FragmentTransaction transaction = getFragmentManager()
.beginTransaction();
if (addToBackStack) {
transaction.addToBackStack(null);
} else {
getFragmentManager().popBackStack(null,
FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
transaction.replace(R.id.fragment_layout_container, fragment);
transaction.commitAllowingStateLoss();
getFragmentManager().executePendingTransactions();
}
and Used it like this,
replaceFragment(new FragmentForgottenPassword(), true);
There is a GitHub library that will do this work for you!https://github.com/rathodchintan/Fragment-Back-StackWhenever you are displaying any new fragment, just push that fragment into stack using following code.
//here this fragment is our first fragment
homeListFragment = new HomeListFragment();
fragmentStack.push(homeListFragment);It has many other stack options too.