getFragmentManager().popbackstack() causing nullpointer exception - android

I am developing an android app with lots of fragments in it. But I am facing problems in switching between them.
Lets say I have a Fragment A now I want to go to Fragment B, for this I am doing like this--
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager
.beginTransaction();
Fragment fragment = new Fragment_B();
transaction.add(R.id.frameLayout, fragment);
transaction.addToBackStack(null);
transaction.commit();
I reach Fragment B successfully. Now I have to go to Fragment C from here, for this again I am doing the same thing.
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager
.beginTransaction();
Fragment fragment = new Fragment_C();
transaction.add(R.id.frameLayot, fragment);
transaction.addToBackStack(null);
transaction.commit();
I do this successfully also. Now I have to revert back to Fragment B, for this I do -
getFragmentManager.popbackstack();
This brings me back to Fragment B. But when I do the same to go to Fragment A now, it causes NullpointerException.
What I am doing wrong here. Why does this run perfectly for the first time but fails at the second time? Please help.

I think you have to check first there are fragment available in backstack or not. follow my below code:
if(manager.getBackStackEntryCount()>0){
manager.popBackStack();
manager.beginTransaction().commit();
}
Thats it...
and yes you do not add fragment a to backstack initally so crosscheck that..

fragmentTransaction.addToBackStack("tag");
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 1) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}

Related

Replace Fragment and press back

Hi Following my code to show detail fragment from list fragment
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.FragmentContainer1, DealFragment.newInstance(dealItems, position, currentPage, totalCount)).addToBackStack(null).commit();
Now when I press back button I get new ListFragment.ListFragmnt state is not saved.
I referred to some stack questions but haven't got right answer
I tried below code but it causes issues when app goes in background and is killed by system(Like I am opening chrome from my detail view and when I go back from chrome my app is closed and minimised) FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.hide(getActivity().getSupportFragmentManager().findFragmentById(containerId));
ft.add(containerId, detailFragment);
ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
Any solution on this problem when I move to ListFragment to detail hot maintain state of detail fragment.
I have referred this link from stack overflow here is the link
I want functionality same as Gmail app when we go from list to detail and come back to list fragment. Scroll position and everything is maintained which is not happening in my case
Try this :
#Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStack();
} else {
this.finish();
}
}
In your activity set the first fragment:
ListFragment listFragment = new ListFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.FragmentContainer1, listFragment );
transaction.addToBackStack(null);
transaction.commit();
Then in listfragment call details fragment:
DetailFragment detailFragment = new DetailFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.FragmentContainer1, detailFragment);
transaction.addToBackStack(null);
transaction.commit();

Retain same instance of Fragment on configuration Change

I have the following issue. My application has 2 fragments, the first one that is executed show a list of items, when you tap on an item, shows a detail fragment.
I also have a NavigationDrawerActivity, with the following methods to handle the stack of fragments.
private void changeFragment(int pos, Fragment fragment, boolean addToBackStack) {
Fragment f = getSupportFragmentManager().findFragmentByTag(String.valueOf(pos));
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
mCacheServices.setmFragmentTag(String.valueOf(pos));
ft.replace(R.id.container, fragment, String.valueOf(pos));
if ((addToBackStack)) {
ft.addToBackStack(STACK_KEY);
}
ft.commit();
}
The problem i am facing is when i am in the DetailFragment, and i rotete the device, it goes to the ItemList fragment, which is the firs fragment excuted by the app. What can i do to mantain on the DetailFragment when i rotate the device?
Thanks!
you can save the state of the fragment and then re-create it. Go through this for more details
http://developer.android.com/guide/topics/resources/runtime-changes.html
hope this helps.

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();

Why I got onBackPressed issue in Android

when I click on the back press on real device it went to the home page not to the previous page ,here I am Using fragments how to solve that issue
In First Fragment
NotesFragment notes = new NotesFragment();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.day_fragment_mainLayout, notes);
transaction.addToBackStack(null);
transaction.commit();
in Second Fragment
DayFragment day = new DayFragment();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.addToBackStack(null);
transaction.replace(R.id.day_fragment_mainLayout, day);
transaction.commit();
in Third Fragment
ItemsFragment items = new ItemsFragment();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.addToBackStack(null);
transaction.replace(R.id.day_fragment_mainLayout, items);
transaction.commit();
when i click on backpress button its goes to the home page,bt i need prevoius page.
note: btnclick i am using to navigating fragments one to one
You could achieve it by using FragmentTransaction's add() method, and then override onBackPressed where you have to pop your FragmentManager's back stack. This will result in the behaviour you described.
You cannot go back because you didn't open any new activity. OnBackPressed is moving you to the previous activity stored in the stack. If you want to go back to the previous fragment, you have to store the previous fragment somewhere and then use:
#Override
public void onBackPressed() {
// here you should change the fragment
transaction.replace(YOUR_PREVIOUS_FRAGMENT, items);
transaction.commit();
}
Thanks for attention!

Popping fragments out and keeping them single instances

I am trying to solve the back button between my fragments in the activity.
for that, I have done:
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.content_frame, new HomeFragmentFeed());
transaction.addToBackStack(null);
transaction.commit();
And to handle the backs:
#Override
public void onBackPressed() {
FragmentManager fm = getFragmentManager();
if (fm.getBackStackEntryCount() > 1)
fm.popBackStack();
else
super.onBackPressed();
}
Problem is that if I navigate between the fragments without popping them out, each can be multiple times in the stack.
How can I solve the issue in which each fragment should be only ONCE in the stack.
Thanks!
I don't think that is possible so What i suggest you do is clear the backstack every time you make a fragment transaction. In this way only last fragment will be in the backstack
popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)

Categories

Resources