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");
Related
i am trying to replace a fragment listview with a new fragment when i am use onclicklistener. However, when i click in one of the listview items change fragment but the listview appers below. Here my code:
changing listview fragment
ent_qtb_operaciones user = listaqtb_operaiones.get(pos);
qtb_operaciones_deta fragment = new qtb_operaciones_deta();
Bundle bundle = new Bundle();
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = manager.beginTransaction();
bundle.putSerializable("usuario",user); // use as per your need
fragment.setArguments(bundle);
fragmentTransaction.replace(R.id.qtb_listview_layout,fragment);
fragmentTransaction.commit();
the fragment which receive inforamaition
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View vista = inflater.inflate(R.layout.qtb_operacion_base, container, false);
return vista;
}
As result, Second fragment superpose first one but listview of first fragment does not desappair
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;
}
I apologize for my English ..
I am new to android development and am working with Fragments.
The doubt is as follows:
My first fragment has 2 buttons, button A and button B. Both lead for a second fragment with a list of items to be selected. Up to this point all right.
What I wonder is after selecting any item in the list must go to the third fragment then how to check if the A button was pressed to take the Fragment A or B button was pressed to take the Fragment B?
You must be calling the second fragment on Button click.
Do this inside onClick() method
Bundle bundle=new Bundle();
bundle.putString("selectedButton", "A"); // or B if button B is clicked
SecondFragment fragobj = new SecondFragment();
fragobj.setArguments(bundle);
getFragmentManager().beginTransaction().replace(R.id.container, fragobj).commit();
and to receive in second fragment's onCreateView() method like this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
String selectedButton = getArguments().getString("selectedButton"); // would give you A or B as passed
return inflater.inflate(R.layout.fragment, container, false);
}
When you create a Fragment you can pass it parameters.
For example:
public static MyFragment newInstance(int someInt) {
MyFragment myFragment = new MyFragment();
Bundle args = new Bundle();
args.putInt("someInt", someInt);
myFragment.setArguments(args);
return myFragment;
}
After that, in the onCreateView() you can receive them
getArguments().getInt("someInt", 0);
When you create the second fragment pass it a parameters that indicates if the "launcher" was button A or B
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();
}
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