How to get top Fragment in backstack from DialogFragment - android

I'm using navigation to navigate between fragments.
And I have a DialogFragment, it can called from from many fragment like this:
val dialog = FragmentDialog
dialog.show(childFragmentManager, "home_fragment")
And I want to know dialogfragment called by which fragment
I tried with FragmentManager.backStackEntryCount but it's seem doesn't work
Can I have a advice for this problem ???

There are multiple ways to do this ..
You can just Pass a key in Bundle to your DialogFragment to check which Fragment opened it ..
If your Dialog fragment a attached to a fragment in each case you can just use getParentFragment() . getParentFragment() will return the instance of that fragment. If its attached to activity then getParentFragment() will be null so watch out for this case also .
you can also use getTag() from where it was open byt passing different tags to transaction .

Related

Android. How to avoid fragment be recreated when back from another fragment?

I'm using MVP in my project. I have an activity. Inside this activity I'm showing fragment. Fragment contains some data, edit text, buttons etc. From this fragment it is possible to navigate to another fragment. Here is my code for showing another fragment:
getParentFragmentManager()
.beginTransaction()
.replace(R.id.main_container, secondFragment)
.addToBackStack(null)
.commitAllowingStateLoss();
Next, when I try to go back from fragment 2 to fragment 1, my fragment one is re-created.The method onViewCreated() is called again with saveedInstanceState = null. Also in the onViewCreated() I call presenter.onCreate(savedInstanceState) and because of this, all requests that should be called when I first enter the fragment are re-called when I back to first fragment.
As far as I understand, when calling a .replace(container, fragment), I cannot avoid recreating the fragment view, but in this case, how can I save all my data in the presenter so that I do not re-execute all requests when returning to the fragment?
P.S. With .add() fragment is not recreated. But in my activity I have toolbar with title, and I don't need it to show in my second fragment. When I open my second fragment with .replace() toolbar is not showing, but when open with .add() toolbar is showing.
Use Fragment Result API :
For implemention this method set - setFragmentResultListener - in source fragment and also set - setResult() and finish() - in destination fragment
Note 1 : for having clean code i suggest you to use FragmentTransaction class in:
https://github.com/mahditavakoli1312/FragmentTransaction---mahdi-tavakoli
Note 2 : use Navigation for navigate betwin fragments and activities Instead tranastion

How do I update my EditText of a fragment from another fragment?

I have two fragments, in which on first fragment there is 5 edittext box and in second fragment there is 3 edittext box and one button . On clicking of fragment two button , I want to generate alert message on 5 edittext box which are in first fragment .
Can anyone help how can i achieve this ??
Early reply is appreciable
You can get the second fragment by tag using fragment manager, and then do whatever you want to its views. be careful of NPEs though.
First, when you call the second fragment you should set a tag:
SecondFragment fragment = SecondFragment.newInstance();
getActivity()
.getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragments_container, fragment, "second_fragment_tag")
.addToBackStack(null)
.commit();
Then, you can access it from the first fragment using the following:
SecondFragment fragment = (SecondFragment)getFragmentManager().findFragmentByTag("second_fragment_tag");

Android how to send data to a running fragment

is there a way to send some data from an activity to a running fragment?
In my app I'm adding a second fragment over another fragment. I intentionally use the add method instead of the replace method. So now I want to hide my second fragment with
fragmentManager.popBackStack();
and my first fragment reappears. After hiding the second fragment I want to send some data from my activity to the still running frist fragment.
Any idea how to do this? It doesn't work with bundles (put extra), because I don't rebuild the fragment, I just hide the second one!
one simple solution is:
MyFragment oldFragment = (MyFragment) fragmentManager.findFragmentById(R.id.fragment_place);
fragmentManager.popBackStackImmediate();
MyFragment newFragment = (MyFragment)fragmentManager.findFragmentById(R.id.fragment_place);
newFragment.postData(...);
You can use an EventBus library like this one, it's easy to use and very convenient.
You can use tags on the Fragments when you create them to call them when needed.
getFragmentManager().beginTransaction()
.add(R.id.content, new SomeFragment(), SomeFragment.class.getSimpleName())
.commit();
So above I use the simple name of the class to tag the fragment when I create and add it to the activity.
SomeFragment fragment = (SomeFragment) getFragmentManager().findFragmentByTag(SomeFragment.class.getSimpleName());
And I can call it back when I need it and know it is being displayed like above, now I can call send it data like normal by calling a public method in the custom fragment and passing it the data as a param.

Communicating between fragments in Android

Based on the example from http://developer.android.com/training/basics/fragments/communicating.html I tried to reproduce the communication between two fragments which are sub fragments of a larger fragment.
In the example, AB activity contains A fragment and B fragment. But I am trying to achieve the same but in my case AB Fragment contains A fragment and B fragment.
The problem is the overridden method in the AB Fragment never gets called. Does this not work because the containing component is a Fragment and not a Activity like in the example? Am I missing out something here?
If you are referring to onClick() or some other onSomething() handler, then these always get called in the Activity class, not the fragment. So in the example you linked, the onArticleSelected() must remain in the Activity, even if you have nested fragments.
To pass info on to the fragment, you have a few options. One, you can keep a reference to the fragment within the activity. This might be lost if your activity recreates (settings event for example).
The second and better way would be to tag your fragments, and then use findFragmentByTag.
When you add your fragment (notice the parameter "my_fragment" which is the tag I gave to the fragment):
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, myFragment, "my_fragment").commit();
Or when you replace one fragment with another:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, myFragment, "my_fragment").comit();
Then, when you want to do something in the fragment from within your onArticleSelected of the activity:
Fragment fragment = getSupportFragmentManger().findFragmentByTag("my_fragment");
if (fragment != null) {
fragment.articleSelected(articleId);
}
You can always use an Interface to communicate between fragments. It is the safest way to do so.

OptionsMenu appears in double

I Have a Fragment inside an Activity. This Activity contains OptionsMenu.
I use a FragmentTransaction to launch a new instance of the same Fragment class, when I selected the OptionsMenu, it appears in double because it load twice.
How can I do to manage OptionsMenu between the Fragments
Thanks.
PS : Sorry for the English
There is no way such way. You can handle on BackPressed and show your own custom menu as a new fragmnet. Or manage your options menu inside of FragmentTransaction inactivity in dependent of your fragment class and content
I find a solution,
I create the OptionsMenu only in the Activity.
I get the OptionsItemsSelected on the Activity too.
When onOptionsItemSelected() occur, I get the current Fragment like this :
FragmentManager fg = getSupportFragmentManager();
Fragment frag = fg.findFragmentById(R.id.fragmentMap);
All my Fragment have the same id but I always get the Fragment on the top.
Then, I can call :
frag.onOptionsItemSelected(item);
to the Fragment receive and my UI is correctly update.
I hope It helps.
Try playing with setHasOptionsMenu(boolean) in your Fragment extending classes,
if that is set to true the FragmentManager should automatically call your current fragment's onOptionsItemSelected,
it set to false you can manually call those callbacks yourself in your Activity's onOptionsItemSelected.
Worked for me... :)

Categories

Resources