Based on String Array item, show same item to next fragment - android

I have two fragments ListNav and SwipeNav in Navigation Drawer. ListNav shows list view of data from String array. SwipeNav shows data as swipe views of the same data from string array. I am able to replace fragment from ListNav to SwipeNav. String array data shows 'A to Z'.
My problem is when I click on list in ListNav (Suppose B)then SwipeNav(replaced Fragment) shows swipe views from start(means A). Suppose, if I click on 'D' in ListNav through Fragment replace, SwipeNav will show 'D'. Plz suggest me how I will implement this.
String Array:
<string-array name="tab_titles">
<item>A</item>
<item>B</item>
<item>C</item>
<item>D</item>
<item>E</item>
<item>F</item>
<item>G</item>
<item>H</item>
<item>I</item>
........
</string-array>
I ListNav I use below to replace to SwipeNav with OnItemClickListener:
FragmentManager fm=getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(((ViewGroup)(getView().getParent())).getId(), new SwipeNav());
ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
SwipeNav shows Data as Swipe Views through PagerAdapter from String Array:
tabTitlesArray = context.getResources().getStringArray(R.array.tab_titles);

I thing you are missing one step there and that is you need to send data in String form when swipe fragment is replacing .
Like.
Bundle bundle = new Bundle();
bundle.putString("TAG", "D");
// set Fragmentclass Arguments
SwipeNav fragobj = new SwipeNav();
fragobj.setArguments(bundle);
FragmentManager fm=getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(((ViewGroup)(getView().getParent())).getId(), fragobj);
ft.addToBackStack(null);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
now on your swipe fragment make sure you are receiving that string that you are passing when creating .
so in onCreateViewmethod .
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String passingString = getArguments().getString("TAG");
return inflater.inflate(R.layout.fragment, container, false);
}
Here TAG just identifying what you are passing .

Related

How to handle fragment load again and again

In my app, I have navigation bottom and container above navigation. I using multiple fragments in one container. When I click on an item of navigation bottom, I replace the fragment to show.
This is my code that shows fragment in the container
private void showFragment(String tabType) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
baseTabBottomFragment = new BaseTabBottomFragment();
//send data to fragment
Bundle bundle = new Bundle();
bundle.putString(KeyConstant.TAB_BOTTOM, tabType);
baseTabBottomFragment.setArguments(bundle);
ft.add(R.id.frame_container, baseTabBottomFragment, tabType);
ft.addToBackStack(tabType);
ft.commit();
}
But I don't know, how to handle when I click on the item navigation bottom, It's always loaded fragment again and again. I want to handle it. If fragment loaded, I don't want to load it again.
Please help me
Sorry for English grammar
1-You can play with that tag tabType e.g:
String prevTab = "";
private void showFragment(String tabType) {
if(!tabType.equal(prevTab)){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
baseTabBottomFragment = new BaseTabBottomFragment();
//send data to fragment
Bundle bundle = new Bundle();
bundle.putString(KeyConstant.TAB_BOTTOM, tabType);
baseTabBottomFragment.setArguments(bundle);
ft.add(R.id.frame_container, baseTabBottomFragment, tabType);
ft.addToBackStack(tabType);
ft.commit();
}
prevTab = tabType;
}
2-Or from your BottomNavigationView you can check for selected item id, e.g:
if (bottomNav.getSelectedItemId() != R.id.someMenuId)
// replace the fragment
3- Or use onNavigationItemReselcted add from Api 26 to know if the current item got selected again or not.

Detecting when a fragment is on top again (has focus)

In my app, my activity has a container with a fragment. When a button in that fragment is clicked, I add a new fragment to the container and add the previous fragment to the backstack.
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.primary_fragment_container, homeFragmentHandler);
fragmentTransaction.addToBackStack(folderId.toString());
fragmentTransaction.commit();
The result is that pressing back closes the top fragment and returns me to the previous fragment, which is exactly how I want it. HOWEVER there are certain elements on the fragment that should be refreshed at this point. When the user clicks back and is returned to the previous fragment, how do I know to refresh the data within that fragment? is there a method such as onResume() for this scenario?
You have to proper add fragments to backstack:
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
MyFragment fragment = new MyFragment ();
transaction.replace(R.id.primary_fragment_container, fragment).addToBackStack(null).commit();
Fragment homeFragmentHandler= new Fragment();
Bundle bundle = new Bundle();
//replace this line below wth something convinient
bundle.putInt("key", value);
fragment.setArguments(bundle);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.primary_fragment_container, homeFragmentHandler);
fragmentTransaction.addToBackStack(folderId.toString());
fragmentTransaction.commit();
This will send data regarding which fragment/ activity caused the previous fragment to load. Now in its onCreate add this code to check the data in the bundle
//In the onCreate of the original fragment
Bundle bundle = this.getArguments();
if(bundle.getInt(key, defaultValue)!=null{
int myInt = bundle.getInt(key, defaultValue);
// Load the data that needs to be refreshed when original fragment is loaded from the second one
}
No else statement is needed. If the fragment is made from any other activity/ fragment then the bundle.getInt will return null and hence that code inside the statement wont be executed.

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

Pass data between two fragments in same activity and update WebView

Hello I'm trying to pass a String value from a fragment to another one. I have a single Activity.
The thing that I'm trying to do is when a listview item is pressed send the value to the another fragment, load the fragment (This I had made with this code):
Fragment cambiarFragment = new FragmentInterurbanos();
Bundle args = new Bundle();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction
.replace(R.id.container, cambiarFragment)
.addToBackStack(null)
.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
// Commit the transaction
transaction.commit();
And then retrieve the String and update a WebView placed in the new fragment (This method cannot be launched when the user select in NavigationDrawer this section (Fragment).
Thanks for your help in advance!
There are many ways you can achieve this... you could pass the string to the new Fragment through a Bundle like this:
Bundle bundle = new Bundle();
bundle.putString(key, value);
fragment.setArguments(bundle);
or maybe have a DataController class where you store the string and the new fragment retrieves it.

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