Getting data from Fragment upon parent activity Button Click - android

my android app main activity contains two fragments.The main activity also contain two buttons-save and cancel.The fragments contain EditTexts and Spinners.i want to take data from these EditTexts and Spinner upon Save button click event(Event occur in parent activity).Is it Possible?

You are able to get the current fragment by doing getFragmentManager().findFragmentById(R.id.fragmentId) that will return the current fragment. You can then check its type by calling getClass() or by checking its instance as such if(getFragmentManager().findFragmentById(R.id.fragmentId) instanceof MyFragment){
//Code goes here
} You can then type cast it if it is the correct class and call whatever methods you want
MyFragment fragment=(MyFragment)getFragmentManager().findFragmentById(R.id.fragmentId);
fragment.getSomeData();

Related

Preserve the state of fragment when previous button is clicked

My android application have BottomNavigationBar. It contains 5 fragments. One of its fragment contain nested fragment which is mutiple step process.
first nested fragment contain next button.Second nested fragment contain previous and next button.Third nested fragment contain previous and submit button. Each fragment have different EditText.
After adding the values in first fragment, when i click next button it goes to second fragment. In second fragment when i click previous button it goes to first fragment again and same process applies to second and third fragment
My questions is:
1)When previous button in second fragment is clicked, i want all the values of EditText in first fragment as it is and when again next button of first fragment is clicked, i want all the values of EditText in second fragment as it is. Is there any way to do this?
2)I want all the EditText values of all nested fragments when user clicked on submit button in third fragment.How to do that?
Yes,
This can be achieved using two ways,
1) Fragment savedInstanceState
https://stackoverflow.com/a/17135346/7316675
2) Keep you values stored at some activity or application level, and access it on resume of fragment screen
You could use a Bundle to store the values and then restore them when you restore the fragment. You could either do this in onStop() (recommended) or onPause().
Private static final String KEY_ADDRESS = "ADDRESS" ;
#Override
Public void onstop(){
Bundles state = new Bundle ();
String address = etv1.getText().toString();
// Get more strings from the etvs
state.putString(KEY_ADDRESS, address);
// Store more strings into the bundle
setInitialSavedState(state)
}
To restore the values you use either the saved instance state the system passed to onCreate() , or pass the bundle to a self created public bundle in onCreate() and access in onResume() like so:
String address = bundle.getString(KEY_ADDRESS);
As for the results being passed you could communicate the bundles to their parent activity whenever the next or previous button is pressed and do with it as you please when submit is pressed. Learn more on How to do that from the docs or this answer on how to do that
Solution of my first question.
I have used popBackStack() method of FragmentManager. Using this i can go back to the previous fragment of stack.I have added this code in OnClickListener of previous button
FragmentManager fm = getActivity().getSupportFragmentManager();
if(fm.getBackStackEntryCount()!=0){
fm.popBackStack();
}
Solution of my second question:
use method setArguments() to set the values and getArguments() to get the values

Changing variable's value of fragment from the activity

I have an activity like this:
There are 2 buttons A and B on toolbar and a frame for fragment to take over. Say I have a string variable named button_type in fragment.
I want to have a system so that when I click button A in activity, the button_type in fragment sets to A and when I click button B in activity, the button_type in fragment sets to B.
How to do this?
Please note that I may click the buttons (A,B) when the fragment is already active (its not like after I click one button, the fragment comes.)
Thanks in advance.
Edit:
Currently I am trying this:
In MainActivity I get similar string button_type and set it as A or B according as button click and use the method:
public String getData(){return button_type;}
And in fragment I use: button_type= activity.getData(); in onViewCreated.
But it only seems to have the initial set value of A and B (which is A) and does not change when another button is clicked.
I think the best way if you use an interface for managing text in the fragment. the fragment will implement the interface. When the button click call the function in the fragment which is implemented by fragment.
Another way you can create an object of the fragment using findFragmentByTag() or findFragmentById() and then call the function in the fragment which handles the text.
Create an interface with methods onClickA(String buttonType) and onClickB(String buttonType).
Then create an object that implements this interface (or make fragment implement this interface by itself). I'll call this object listener.
call setButtonType(String buttonType) in listener methods implementation.
Then pass your listener to activity (you can get an instance of parent activity in fragment with getActivity() and cast it to your activity class) and in onClickListener of button A (in activity) call listener.onClickA(yourString) and do the same thing for button B.

Passing multiple values from all fragments to its container activity by onClick event

How can I get multiple editText value from all fragments by onClick event which located in its container activity? Those EditText are being valid and checking by TextWatcher. I can't get any value of editText by implementing inflate and return fragment's layout to its contain activity.
What is more, how can I handle the situation of passing editTexts' value when all editTexts are valid if using Interface on fragment.
Thanks for any help.
There is several ways
Use SharedPreferences where you save all the value first and
then retrieve it later in activity.
Use interface class as bridge communication between fragment and activity
For Example:example interface class

Android : Get previous fragment name to do nothing in onCreate

I have a detected a bug in my app today : When a user clic on an item in my listview, it launch a new fragment with details about this item. But when the user is in this fragment, if he change orientation of the device, the onCreate method of my first class (which fetch my listview) is called, and the app crashed.
I would like to know if it's possible to get the name of the previous fragment, in order to add a test like the following in my onCreate method :
if (!fragmentName.equals("common")){
Log.d("INFO", "Do nothing in this case, because user was in detail fragment before !"),
} else {
super.onCreate(savedInstanceState);
refreshList(true);
}
I don't think it is possible to get the name of the previous fragment as its creating a new instance of your Fragment object.
If I am right in what you are trying to do you need to somehow store the object for which you are displaying data so that when the Fragment is recreated you can then retrieve the required object. One way is you keep the ID or a unique property of the object in SharedPreferences and then reacquire its data when it reloads from a singleton somewhere.
Basically you may need to change the way you retrieve your data or pass it from the original opening Fragment so that you can retrieve it again if the second Fragment is destroyed.

how to force a fragment to re run its own onCreateView

I have a tabHost which hosts 4 fragments.When the tabHost activity is created it creates and includes the fragments.How can i force a fragment to re create its view?meaning to re run the on createView.
EDIT: My first fragment consumes a web-service and updates a field in the parent activity.That field is read by my second fragment.But by the time the activity is instantiated the field in the parent activity is empty.So that's the reason I asked my question.I want the second Fragment to be re-drawn after the field is updated.Hope I'm clear now.Thank you for your time.
What you need to do is use the Observer (or Listener) design pattern. Basically what you do is create an interface like this (this is just an example)
interface DataListener {
void dataHasArrived(Object data);
}
Your second fragment (the one that shows the data) should implement this (the object in the argument is the data that you want to present). The fragment that gets the data should have a reference to it (a field of type DataListener that contains the first fragment, you can set it in the parent activity). When the data is available you just call the method from the first fragment with the data as the argument and the implementation in the second fragment does the rest.

Categories

Resources