Handeling events in nested fragments - android

Here is my problem, I am opening a new fragament from first fragment's Gridview item click. In my 1st fragment to refresh the items of grid view. I have implemented swipetoRefresh functionality.
But when I am opening the 2nd fragment to show my grid items in horizontal scrollview. Here also if I do swipe to refresh(swaping my finger from top to bottom). Then my first fragment event occurs. I dont want my first fragment event on 2nd fragment. Please let me know how can I resolve it.
Below is my code to open new Fragment.
Fragment newFragment = new SwipeFragment(featureArr, dataList, HomeFragment.this);
// Log.e("position clicked is","-----"+position);
Bundle b = new Bundle();
b.putInt("pos", position);
newFragment.setArguments(b);
FragmentTransaction transaction = ((AppCompatActivity)mContext).getSupportFragmentManager().beginTransaction();
transaction.add(R.id.frame_edit, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();

Related

Android: Replace sub fragment with it's parent fragment in main activity container

Imagine i have main activity that has viewpager. I have 2 fragments called (F1 & F2) that will transaction into viewpager.
Again imagine in F1 fragment i want to set a button. When clicking on button, i want to transaction other fragment call SUBF1 but not into F1 fragment.
My question is here!!! Is it possible to replace SUBF1 with it's parent means F1?My idea is that i want to replace sub fragment with it's parent fragment that has been kept on fragment's container in main activity?
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.details, new NewFragmentToReplace(), "NewFragmentTag");
ft.commit();
You can save the instance of current fragment, when you are
navigating from one fragment to other. When user press the back
button, you can open the specific fragment with the help of tag.
#Override
public void onBackPressed() {
String currentTag = currentFragment.getTag();
if(currentTag.equals(res.getString(R.string.fragmentTagTest)))
{
currentFragment = AnotherFragment;
replaceFragment() // integrate replace current fragment with new one.
}
}

Added fragment scrolled down

I have two fragments the first fragment contains list of linear layouts and the whole fragment is in scroll view the second fragment is added and the first is hidden on choosing item from the first.
The problem is the second fragment is created scrolled down if the first fragment was scrolled down.
I tried the ways to force second fragment to be scrolled to (0,0) but failed.
the code used to add the second fragment
public void setActionOnClick(String id) {
CommentFragment frag = new CommentFragment();
Bundle bundle = new Bundle();
bundle.putString("id", id);
bundle.putString("TAG", TAG_NEWS_STORY);
((MainActivity) getActivity()).setCurrentTag(TAG_NEWS_STORY);
frag.setArguments(bundle);
android.support.v4.app.FragmentTransaction fragmentTransaction =
getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(android.R.anim.fade_in,
android.R.anim.fade_out);
fragmentTransaction.add(R.id.main_content, frag, TAG_NEWS_STORY);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
on Attach of second fragment the first fragment is hidden.
I don't want to use fragmentTransaction.replace because there are calls to the api which I don't want to reload.
this is an old question but. probably your fragment place_hoder is inside an scrollview.
just check if R.id.main_content is in a scrollview or not.
you should add the ScrollView to the fragment-layout instead of instantiating the fragment inside one
Before this:
android.support.v4.app.FragmentTransaction fragmentTransaction =
getActivity().getSupportFragmentManager().beginTransaction();
Add code to remove old fragment:
getSupportFragmentManager().beginTransaction().remove(yourOldFragment).commitAllowingStateLoss();

Android ListView is reloading all of the items (rather than just appending the new item) after I return back from a fragment

Android ListView is reloading all of the items (rather than just appending the new item) after I return back from a fragment where i inserted a new item onto the listview (in another fragment, ListFragment)
I have the following code to launch a fragment through Navigation Drawer:
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
SherlockFragment frag = null;
// Locate Position
switch (position) {
case 0:
frag = ListFragment.newInstance();
ft.replace(R.id.content_frame, frag);
break;
case 1:
frag = InsertNewsFragment.newInstance();
ft.replace(R.id.content_frame, frag);
break;
default:
frag = UnderConstructionFragment.newInstance();
ft.replace(R.id.content_frame, frag);
break;
}
ft.commit();
listview.setItemChecked(position, true);
drawlayout.closeDrawer(listview);
The issue is that when I initially load the activity ListFragment is launched, but when user opens the navigation drawer and selects the add News, I replace the current fragment with the InsertNewsFragment, the insertNewsFragment adds data to the adapter that is displayed in the ListFragment. After adding the news element, the user selects the list fragment option in the navigation drawer, but I would expect the same listview to be there and just append the new data to the listview. But what happens is that the whole listview is loaded from scratch again. Can show me the way to how item can be appended to the list view when I return from another fragment, and not reload the whole listview. Thanks in advance :)
you cannot do this as you are replacing the listfragment. Initially you will have to check if the listview is opened if it comes out to be true then close all the other fragments that are opened and bring it (The listview fragment) to the top then dont set the adapter again and again just use the notifydatasetchanged method

Remove fragments from detail view

I wanted to remove fragments(stack of fragments) from a master-detail's Detail view. If i click on a list item in the master view, it opens up a fragment, if i edit the list view's item another fragment is used.
What i want to do is, remove all the fragments from the detail view changing it to a blank container, as it is when the app starts up.
Currently i am replacing the container with a blank fragment, but it does not work in few detail view cases.
NullFragment fragment = new NullFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.item_detail_container,
fragment);
fragmentManager.popBackStackImmediate();
fragmentTransaction.commit();
You can use FragmentManager.getBackStackEntryCount() to get number of fragments in backstack and use FragmentManager.popBackStack() to pop the top fragment in stack one-by-one.

Android added Fragment to screen, but taps fall through new Fragment view to the previous one

I'm working with Fragments for the first time and running into a weird behavior. I have an activity with a Fragment covering the entire view. It contains a ListView. When I tap on an item in the ListView, I want to show a details fragment which contains information about that item.
This is how I'm presenting the new fragment view:
FragmentManager fragmentManager = this.getActivity().getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
DetailsFragment fragment = new DetailsFragment();
transaction.add(R.id.viewRoot, fragment).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack("Details").commit();
R.id.viewRoot is the id of the root layout in the first Fragment's layout xml, so the new Fragment fills the entire screen.
My Problem: When the new fragment is visible and covering the entire screen, any clicks or taps which land on the background (i.e. not hitting a button or textfield on the new fragment) appear to be going through the view and landing on the first fragment. So, I tap on a ListView item, which causes the DetailsFragment to get added to the screen. Then when I tap on the background of the DetailsFragment, that tap falls through and lands on the ListView causing another DetailsFragment to be added for whatever item I happened to hit behind the scenes.
Am I adding my new fragment incorrectly? Why are my clicks/taps falling through?
EDIT for caiuspb:
public class BaseFragment extends Fragment {
public void pushNewFragment(Fragment fragment, String description) {
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.remove(this);
transaction.add(R.id.viewRoot, fragment).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(description).commit();
}
}
All of my fragments extend my BaseFragment class.
I experimented with the suggestions I received, but none of them worked. In the end, my solution was to add an OnTouchListener to the root view of my subfragments which discarded all touches.
View view = inflater.inflate(R.layout.mylayout, container, false);
view.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
There is another post here on StackOverflow about that strange Fragment behaviour. Try to remove your Fragment before you add it because the replace Method seems to be buggy
edit:
Move the Fragment logic into your Activity because your activity contains your Fragments. Today I tried to use the FragmentActivity and Fragments from the Android Support Library. Now I can use the replace method withouht any bugs. This is my approach and this works:
public class DashActivity extends FragmentActivity{
...
private void changeRightFragment(Fragment fragment) {
Log.i(TAG, "Loading Fragment into container: " + fragment.getClass().toString());
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.rightfrag, fragment);
// Add a transition effect
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
// Add the Fragment to the BackStack so it can be restored by
// pressing the Back button
ft.addToBackStack(null);
ft.commit();
}
...
public void setUserFragment(User user) {
UserFragment fragment = new UserFragment(user);
changeRightFragment(fragment);
}
So if you want to invoke a changeFragment method from inside your Fragment use a callback mechanism to the Activity

Categories

Resources