I have two fragments like left panel one fragment and right side another fragment having.
The left panel fragment i having the add button from there when i click add button it launches another activity, from this activity i am trying the access the fragment but i am not getting.
This is the code i am using in my activity
LeftFragment left = (LeftFragment) getFragmentManager().findFragmentById(R.id.fragment1);
finish();
Can anyone help me.
Well, this because you can't get access to FragmentManager of other Activity, and it`s absolutely normal.
You can simply commit second fragment to the same container as a first one (use single Activity) and use method setCustomAnimations(...) for your transaction to animation.
Good luck!
you can define another ListFragment in your Fragment like this:
SecondListFragment SecondListFragment= (SecondListFragment )getFragmentManager().findFragmentById(R.id.second_list_fragment);
SecondListFragment.SetupSecondFragmentList();//its written on onListItemClick in FirstFragment
SetupSecondFragmentList() is a function that setup a list view in my Second ListFragment which has called from first one
Related
how to make button to open another fragment. being within a fragment. kotlin
I'm starting in kotlin and I'm having a hard time trying to open a fragment with a button, how do I?
You need to use FragmentManager and FragmentTransaction to add your fragment on the fly. you can call a function similar to this in your button's onClick method. But it is recommended for the parent activity to handle each fragment's lifecycle and the fragments
are not supposed to interact each other. The following is taken from the developer docs, that can be found here.
"Often you will want one Fragment to communicate with another, for example to change the content based on a user event. All Fragment-to-Fragment communication is done either through a shared ViewModel or through the associated Activity. Two Fragments should never communicate directly."
fun createFragmentonTheFly(){
var mFragmentTransaction: FragmentTransaction = getSupportFragmentManager().beginTransaction()
mFragmentTransaction.add(R.id.fr_container,new ProductListFragment())
mFragmentTransaction.commit()
}
The best way to do it would be to add an interface let say onFragmentDetachedLisetner and add one method replaceFragment() or something and make your Activity implement this interaface and had it replace as soon as the fragment is detached and make your fragment that contains your button finish itself when user clicks the button, then your activity will replace it with the one you wanted to start. And also consider reusing fragments, as that is the main purpose of fragments at the first place.
In my MainActivity, I have a viewPager
containing two containers "A and B".
Both containers are possessed with their own Fragments.
In container B; I again have a viewPager containing multiple fragments.(For example one,two,three,four)
On launch of my app MainActivity gets load with container A's homeFragment.
What I need to do is,For a given condition I have to launch fragment two of container B from onResume of my MainActivity.
What I've able to achieve is , I have successfully redirect to container B's fragment one but couldn't able to redirect towards fragment two from onResume of MainActivity, any help will be appreciate.
If you are using Intent to redirect to Fragment B, then pass the int position of sub fragment, and on the fragment B side check for intent extras and set the current page, which you received via intent.getIntExtra().
If I understood it clear, you want to show the second page of the viewpager when you open it, if so you can use mViewPager.setCurrentItem(1, true);
I think you want to set the specific page .. so to do that you can set viewpager as :
viewpager.setCurrentItem(item, true);
Certainly,I found a way of achieving this by using static concept.
I have declared a static variable inside container B's fragment two,and set its value in onResume of MainActivity.
When onResume calls with given condition it sets static variables value to the required fragment, and in my container B,I have used this value to set setCurrentItem() of B'viewPager.
ps: I know it quite confusing as it seems but not too complex in practice,work fine.
So I have a fragment A which has a button to open another a fragment B. In fragment B I can pick some options, which is bundled into an Bundleobject. When I exit from fragment B, I want to refresh a TextView in fragment A.
Right now I'm using dismiss() method to remove the fragment, and then call back the fragment again so that onCreateView() is called. It works fine, but I don't want the animation where the fragment windows is run. So I like to not use dismiss() to remove the fragment instead I want to keep it on the Activity, but I need to know how I can refresh fragment A. I've tried overriding onActivityCreated() but it didn't result in the action I wanted.
So I wonder what's the approach if I want to refresh fragment A without having to dismiss it first so that onCreateView() can be called again.
I can attach code if needed. But maybe just an explanation is enough here?
you can use the life cycle function onResume() in fragment A to update the textview.
You can create your own Listener interface ( example how to do it or this) that listens when you remove your fragment, and you can get the event on Fragment A where you can setText to your TextView.
I have a fragment and an activity.there are some fields and an button in fragment.When i click an button in fragment then i want to go to an activity.In that activity i have another button.when i click on that button i want to return from the present activity to the previous fragment.Every thing works fine.what i need is i want to recreate the fragment when i return to the fragment from the activity..i.e,for example in the fragment if i enter anything in the edit_text before passing to the activity, which is also presented when i returned from the activity.(the fragment is same as how i left from fragment) which is i don't want .i want a fresh page.i want to recreate a fragment when i returned from activity ..i need to select fields newly..can any one suggest any help..thanks in advance..
Try to use main FragmentActivity and Fragments and organize transactions between fragments. I didn't meet examples with Activity and Fragment in one Addlication.
I have an Activity called Mytaskonclick extends Activity and a Fragment called SentTaskFragment extends Fragment. I want to go from Mytaskonclick to SentTaskFragment on click of a button. I tried using the code
Intent ii=new Intent(Mytaskonclick.this,SentTaskFragment.class);
startActivity(ii);
But this code doesn't work. Can anyone suggest me how to do it?
Fragments does not work like this.You need to put a place holder in your activity, say FrameLayout for example and, inside on click, you get the fragment and attach it inside this placeholder. I suggest you read the Fragment Tutorial Here. It contains all what you need to know and extremely useful for you.
When you want to switch to a Fragment them you have to do that through FragmentManager. Pass that thae Fragment object into beginTransaction() method of FragmentManager along with the container layout which will hold the Fragment as below...
SentTaskFragment fragment = new SentTaskFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().add(R.id.frame_container, fragment).commit();
You can see some tutorial from below link...
Android Developer-Fragment
Multi-pane development in Android with Fragments - Tutorial
Android Fragments
Android Fragments Example