Maintaining Backstack for Fragment with ChildFragments android - android

I Have an Activity A which calls a Fragment F1. Now this Fragment calls another Fragment F2 using below code:
Fragment fragment = new F2Fragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.frame_container, fragment);
ft.addToBackStack("fragment");
ft.commit();
Then Fragement F2 calls another Fragment F3 using similar code :
Fragment fragment = new F3Fragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.frame_container, fragment);
ft.addToBackStack("fragment");
ft.commit();
Fragment F3 has 3 child Fragments (for 3 Tabs) and they are added using tabhost as below:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.main_tab, container,
false);
mTabHost = (FragmentTabHost) rootView
.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(),
R.layout.main_tab);
Bundle arg1 = new Bundle();
arg1.putInt("CF1", 1);
mTabHost.addTab(
mTabHost.newTabSpec("Tab1").setIndicator("CF1",
getResources().getDrawable(R.drawable.tab_left)),
CF1Fragment, arg1);
Bundle arg2 = new Bundle();
arg2.putInt("CF2", 2);
mTabHost.addTab(
mTabHost.newTabSpec("Tab2").setIndicator("CF2",
getResources().getDrawable(R.drawable.tab_middle)),
CF2Fragment.class, arg2);
Bundle arg3 = new Bundle();
arg3.putInt("CF3", 3);
mTabHost.addTab(
mTabHost.newTabSpec("Tab3").setIndicator("CF3",
getResources().getDrawable(R.drawable.tab_rigth)),
CF3Fragment.class, arg2);
return rootView;
}
Till this point everything is working fine with proper back navigation.
Child Fragment calls a dialogfragment as below
ConfirmDialogFragment cd = new ConfirmDialogFragment();
cd.show(fm, "Confirm Fragment");
In Dialog I have a button, pressing on which has to refresh the CF1 Fragmnet(from where its called). Its successfully refreshes the CF1 fragment with new list but issue is when I press back button. On pressing back it should go to F3 (from where CF1 was called) but it remains in CF1 with state prior to calling Dialog. Pressing Back again takes it to Fragment F3.
I tried many things but nothing seems to be working for me. I assume that when Confirm Dialog is called from CF1 it places itself on top of Backstack, so when back is pressed from CF1 it resumes to state from where Dialog Fragment got called. I understand if somehow this isn't placed on backstack while calling dialogfragment , this would be resolved but
nothing seem to be working as of now. Please advise.

use below code to remove fragments from backstack
FragmentManager fragmentManager = getSupportFragmentManager();
if (fragmentManager .getBackStackEntryCount() > 0){
fragmentManager .popBackStack();
}

Related

Shared element back animation without backstack

I use one activity multiple fragments in my app. I use a shared element animation. I have two fragments, one of them is a detail page.
I don't want to addToBackStack function for fragment transition. And the detail fragment returns without animation for some cases. (fragmentMain->fragmentDetail)
DetailFragment detailFragment = new DetailFragment();
Transition moveTransition = TransitionInflater.from(MainActivity.singleInstance)
.inflateTransition(android.R.transition.move); // android.R.transition.move
moveTransition.setDuration(400);
detailFragment.setSharedElementEnterTransition(moveTransition);
FragmentManager fragmentManager = MainActivity.getActivityFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction()
.addSharedElement(sharedView, sharedView.getTransitionName())
.replace(R.id.mainLayout, detailFragment);
fragmentTransaction.commitAllowingStateLoss();
fragmentManager.executePendingTransactions();
This code works for me. I know that if I use addToBackStack, fragmentDetail->fragmentMain reveal animation works automatically. But I don't want to the use back stack.
Below code doesn't works for fragmentDetail->fragmentMain.
detailFragment.setSharedElementReturnTransition(moveTransition);
mainFragment.setSharedElementEnterTransition(moveTransition);
FragmentManager fragmentManager = MainActivity.getActivityFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction()
.addSharedElement(textView, textView.getTransitionName())
.replace(R.id.mainLayout, mainFragment);
fragmentTransaction.commitAllowingStateLoss();
fragmentManager.executePendingTransactions();
How can I do shared element animation for this case?
Look at your onCreateView function. If It recreate the layout, it breaks the transition id.
Solution:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
if(inflatedView != null)
return inflatedView;
inflatedView = inflater.inflate(R.layout.fragment_blank, container,
return inflatedView;
}

start Fragment from RecycleView Adapter Onclick

Hi I have A RecycleView Adapter and A button. I want that button to start a Fragment. I can start an activity but not a fragment. I have tried this Onclick method for my Button
#Override
public void onClick(View v) {
Bundle bundle = new Bundle();
bundle.putParcelable("event", events.get(getLayoutPosition()));
Fragment fragment = new EditEventDetailFragment();
fragment.setArguments(bundle);
fragment.getFragmentManager().beginTransaction().replace(R.id.contentMainDrawer,fragment).commit();
}
But have error invoke null object (contentMainDrawer) is my Main activity content_layout.
Any help is much appreciate. The Fragment host recycle view is call from Mainactivity
Use below code for replace fragment
Fragment fragment = new EditEventDetailFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.contentMainDrawer, fragment, "tag").commit();
if you are using this code inside adapter than replace getActivity() to ((Activity) context).
Hi I have found 2 answer that help whoever needed. The answer is to use the Activity that host the calling fragment. Many thanks #Mohit Suthar
Bundle bundle = new Bundle();
bundle.putParcelable("event", events.get(getLayoutPosition()));
Fragment fragment = new EditEventDetailFragment();
fragment.setArguments(bundle);
FragmentManager fragmentManager = ((MainActivity) context).getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.contentMainDrawer, fragment, "tag").commit();

Determine fragment before current fragment

Imagine there is fragment A and fragment B.
if I click the button on fragment A, it leads to fragment C.
if I click the button on fragment B, it also leads to fragment C.
Now I want to detect from which fragment does the fragment C is created. Is it possible?
You can simply set a variable and pass that to Fragment C through bundle while doing FragmentTransaction like this as shown
Fragment fr=new FragmentA();
FragmentManager fm=getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
Bundle args = new Bundle();
args.putString("from", "fragmentA");
fr.setArguments(args);
ft.replace(R.id.content_frame, fr);
ft.commit();
and you can retrieve the same in Fragment C like this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext = getArguments().getString("from"); // with value of strtext,you will get to know from which fragment you come from
return inflater.inflate(R.layout.fragment, container, false);
}
##You can pass a boolean value and toggle between two fragments.##
Bundle args = new Bundle();
args.putBoolean("key",true) // for fragement A
fr.setArguments(args);
ft.replace(R.id.content_frame, fr);
ft.commit();
if(getArguments().getBoolean("key");)
Log.e(LOGTAG,"FROM FRAGMENT A");
else
Log.e(LOGTAG,"FROM FRAGMENT B");

Android. Navigation drawer. Backstack issues in Fragment

I am trying to implement app with navigation drawer.
I created application on Eclipse.
This is default(or template) navigation drawer application.
MainActivity has root FrameLayout which includes different fragments at different times.
By default, it contains fragment A.
When item B will be chosen from navigation drawer, it will replace fragment A to fragment B.
MainActivity:
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.container, fragmentB);
transaction.addToBackStack(null);
transaction.commit();
FragmentB is root fragment for fragmentB2, FragmentB3 etc.
I thought that in this way, it will be easier to save fragments state.
It is saving fragment state only for FragmentB2.
FragmentA(root A) onCreateView():
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/* Inflate the layout for this fragment */
View view = inflater.inflate(R.layout.fragment_balance_root, container,
false);
FragmentTransaction transaction = getChildFragmentManager()
.beginTransaction();
transaction.replace(R.id.balance_root_frame, MainActivity.balanceMainFragment);
transaction.addToBackStack(null);
transaction.commit();
return view;
}
FragmentA2 OnCreateView():
View rootView = inflater.inflate(R.layout.fragment_balance_main,
container, false);
return rootView;
FragmentB(root B) onCreateView():
FragmentTransaction transaction = getChildFragmentManager()
.beginTransaction();
transaction.replace(R.id.tariffs_root_frame, MainActivity.tariffsMainFragment);
transaction.addToBackStack(null);
transaction.commit();
FragmentB2:
private static void goToTariffInfo() {
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.tariffs_root_frame, new TariffsInfoFragment());
transaction.addToBackStack(null);
transaction.commit();
}
Fragment B3:
View rootView = inflater.inflate(R.layout.fragment_tariffs_info,
container, false);
return rootView;
I have many questions, but they are related to each other.
1. How to avoid saving root fragment in backstack?
When I press back button on Fragment A2 it goes to A1 which is blank.
I have tried to remove addToBackStack from MainActivity. This caused to quit on only one press back button.
I have tried to remove addToBackStack from FragmentA. No effect.
2.How to set seperate backstack for different collections of fragments?
For example: Navigation drawer includes 2 items: ItemA and ItemB.
Each item can be considered as one section.
FragmenA->FragmentA2->FragmentA3
FragmentB->FragmentB2->FragmentB3
But in my situation, when I click on back button on Fragment B2, somehow app goes back to Fragment A2(which previously was opened)
Even on Fragment B3, back button causes to go to Fragment A2.
How to solve these problems?

how to move from one fragment to another fragment on button click

I am using the SherlockFragments library for the sliding menu.I have list of items as menu when
I click on item fragment get opened as an activity but it is a fragment.Now I am new to fragments.i don't know how to move from one fragment to another fragment.As in activity, we have intent to move to another activity.but in fragment I don't how to move to another fragment.I have a button in fragmentA.when I click on this button it moves to fragment B.
By googling I came to know that it has different cycles but anyhow I get toast msg when I click button
here is following code
public class Fragment2 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.actionnetworklogin, container, false);
Button login = (Button)view.findViewById(R.id.login);
login.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(v.getContext().getApplicationContext(),"login clicked", 5000).show();
}
});
return view;
}
}
Can someone please tell me how can I move one fragment to another fragment?
Is there any other method that I can use activities instead of fragments?
I have written code for all activities and java files but I don't know that sliding menu has fragmented and now I have to write all the code fragments.
It's simple Only three line code...
Fragment fragment = new SalesFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
This is the code I use to switch fragments inside a view:
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace([viewId], fragment, tag);
fragmentTransaction.addToBackStack(tag);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.commit();
You can try this also:-
public void ButtonClick(View view) {
Fragment mFragment = new YourNextFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame, mFragment ).commit();
}
FragmentManager fragmentManager = getFragmentManager();
fragmentManager
.beginTransaction()
.replace(R.id.frame_container,
new TeacherFragment()).commit();
FragmentTransaction fragmenttransaction = getSupportFragmentManager().beginTransaction();
FirstFragment regcomplainfragment = new FirstFragment();
fragmenttransaction.replace(R.id.content_frame, regcomplainfragment).addToBackStack("tag");
fragmenttransaction.commit();
//Below is the example
//In first Fragment
Fragment fragment = new YourFragmentClassName();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager(); android.support.v4.app.FragmentTransaction ft= fragmentManager.beginTransaction();
ft.replace(R.id.flContent, fragment);
Bundle args = new Bundle();
// Pass the values what you want to send to next fragment
args.putInt("Year", rYear);
args.putString("Month", rMonth);
args.putInt("Industry", rIndustry);
fragment.setArguments(args);
ft.commit();
//In Second Fragment
//In onCreateView Method get the values
int strYear= getArguments().getInt("Year");
String strMonth = getArguments().getString("Month");
strIndustry= getArguments().getInt("Industry");
//That's it very simple

Categories

Resources