I need to pass some data from the child fragment to the parent fragment that I will be able to read when I go back to the parent fragment. In detail:
I have a FragmentActivity that calls FragmentParent. From FragmentParent I call FragmentChild like this:
FragmentChild fragmentChild = new FragmentChild();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frl_view_container, fragmentChild);
transaction.addToBackStack(null);
ctransaction.commit();
In FragmentChild I set a string value which I need to pass back to FragmentParent and then I return back to FragmentParent.
String result = "OK";
getFragmentManager().popBackStack();
What is the best/proper way to read the result string in FragmentParent?
Android architecture components solution:
In case you are using Android architecture components, it possible to share data between all Fragments of an Activity with a ViewModel. Ensure ViewModelProviders makes use of Activity context to create ViewModels.
public class SharedViewModel extends ViewModel {
private final MutableLiveData<Item> selected = new MutableLiveData<Item>();
public void select(Item item) {
selected.setValue(item);
}
public LiveData<Item> getSelected() {
return selected;
}
}
public class MasterFragment extends Fragment {
private SharedViewModel model;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
model = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);
itemSelector.setOnClickListener(item -> {
model.select(item);
});
}
}
public class DetailFragment extends Fragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedViewModel model = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);
model.getSelected().observe(this, { item ->
// Update the UI.
});
}
}
Non Android architecture components solution:
You can use setTargetFragment and onActivityResult to achieve this.
Set FragmentParent instance as target fragment on FragmentChild instance i.e.
FragmentChild fragmentChild = new FragmentChild();
fragmentChild.setTargetFragment(this, FRAGMENT_CODE);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frl_view_container, fragmentChild);
transaction.addToBackStack(null);
transaction.commit();
In FragmentChild, wherever you are invoking the popBackStack, call onActivityResult on the set target Fragment. Use Bundle to pass on additional data.
Intent intent = new Intent();
intent.putExtra(FRAGMENT_KEY, "Ok");
getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, intent);
getFragmentManager().popBackStack();
Back in FragmentParent, override the default onActivityResult method.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == FRAGMENT_CODE && resultCode == Activity.RESULT_OK) {
if(data != null) {
String value = data.getStringExtra(FRAGMENT_KEY);
if(value != null) {
Log.v(TAG, "Data passed from Child fragment = " + value);
}
}
}
}
Related
I am new to android and I am trying to call my MapFragment from adapter after on click using intent below is my code
Below is adapter code:
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
final BusInfo info = getItem(position);
View view = LayoutInflater.from(context).inflate(R.layout.bus_only_list,null);
TextView busname;
busname = (TextView) view.findViewById(R.id.busname);
busname.setText(info.name);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pref = context.getSharedPreferences("busInfo",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString("bus_name",info.name);
editor.commit();
Intent intent = new Intent(context, MapsFragment.class);
intent.putExtra("name",info.name);
context.startActivity(intent);>
}
});
return view;
}
I want to pass to mapfragment using intent but it redirect to MainActivity instead of MapFragment. How can I stop transferring to MainActivity?
Thank you.
A common pattern to passing a value to a Fragment is using newInstance method. In this method you can set Argument to fragment as a means to send the value.
First, create the newInstance method:
public class YourFragment extends Fragment {
...
// Creates a new fragment with bus_name
public static YourFragment newInstance(String busName) {
YourFragment yourFragment = new YourFragment();
Bundle args = new Bundle();
args.putString("bus_name", busName);
yourFragment.setArguments(args);
return yourFragment;
}
...
}
Then you can get the value in onCreate:
public class YourFragment extends Fragment {
...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the value from arguments
String busName = getArguments().getString("bus_name", "");
}
...
}
You can set the value to the Fragment from your activity with:
FragmentTransaction fragTransaction = getSupportFragmentManager().beginTransaction();
YourFragment yourFragment = YourFragment.newInstance("bus_name_value");
fragTransaction.replace(R.id.fragment_place_holder, yourFragment);
fragTransaction.commit();
You can use the above codes to send the value in Fragment initialization.
If you want to set the value to the already instantiated fragment, you can create a method then invoke the method to set the value:
public class YourFragment extends Fragment {
...
public setBusName(String busName) {
// set the bus name to your fragment.
}
...
}
Now, In the activity, you can invoke it with:
// R.id.yourFragment is the id of fragment in xml
YourFragment yourFragment = (YourFragment) getSupportFragmentManager()
.findFragmentById(R.id.yourFragment);
yourFragment.setBusName("bus_name_value");
You cannot pass an intent to a Fragment. Try using a Bundle instead.
Bundle bundle = new Bundle();
bundle.putString("name", info.name);
mapFragment.setArguments(bundle)
In your Fragment (MapsFragment) get the Bundle like this:
Bundle bundle = this.getArguments();
if(bundle != null){
String infoName = bundle.getString("name");
}
As guys mentioned before:
1. Use callback or just casting on your context (your activity must handle changing fragments itself).
2. To change fragments use activity's FragmentManager - intent is used to start another activity.
Fragments Documentation
I have 3 fragments. Fragment A, B and C. A have a "continue" button which will take it to B. B have a proceed button which will take it to C. C have a "add" button which will take it back to B. Now I want to send data from A to B when the continue button is pressed. and also from C to B when the add button is pressed. I tried using bundle. It is giving me null pointer exception as the first time when going from A to B , the bundle from C is null. How to solve this? Any help is highly appreciated. Please go through the code snippet below
Note: ItemDetails is obtained from fragment A and EmployeeDetails is obtained from fragment C. Fragment Flow => 1. fragment A 2. A to B(itemsList passed to B) 3. B to C (No communication) 4. Back to B from C(Employee List passed to B).
String TEMP_STRING_EMPLOYEES, TEMP_STRING_ITEMS;
EmployeeList employeeList;
ItemsList itemsList;
#Override
public void onStart() {
super.onStart();
Bundle args = getArguments();
if (args != null) {
TEMP_STRING_ITEMS = args.getString("ItemsDetails");
try {
// Set article based on argument passed in
TEMP_STRING_EMPLOYEES = args.getString("EmployeeDetails");
} catch (NullPointerException ex) {
}
} else {
}
}
//Next lines of code from MAinActivity.java
#Override
public void onFragmentInteractionForEmployeeDetails(ArrayList arrayList) {
EmployeeList employeeList = new EmployeeList(arrayList);
String correspondingJson = NavigationUtils.getStringForObject(employeeList);
B newFragment = new B();
Bundle args = new Bundle();
args.putString("EmployeeDetails", correspondingJson);
newFragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
You can use a callback in the Fragment and the Activity stores your object.
Activity
public class MyActivity extends Activity implements MyActivityCallback{
private Object myObject; // Replace with your object type
#Override
public Object getMyObject(){
return myObject;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myObject = new Object();
// ...
}
// ....
}
MyActivityCallback
public interface MyActivityCallback{
Object getMyObject();
}
Fragment
private MyActivityCallback mCallback;
#Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mCallback = (MyActivityCallback) getActivity();
} catch (ClassCastException e) {
throw new ClassCastException(context.toString()
+ " must implement " + MyActivityCallback.class.getSimpleName());
}
}
#Override
public void onDetach() {
mCallback = null;
super.onDetach();
}
Then in your Fragment, to access your object you do mCallback.getMyObject();
I have an activity with bottom navigation tabs that are changing the fragments in it. When I click back and forth on those tabs, at some point it stops working. Code executes just fine as I put some logs in it. But the fragments aren't being switched.
Code is in kotlin but it's rather straight forward
fun showTabFragment(tag: String) {
val currentFragment: Fragment? = supportFragmentManager.fragments?.lastOrNull()
var fragment = supportFragmentManager.findFragmentByTag(tag)
val fragmentExists = fragment != null
if (fragment == null) {
when (tag) {
TAG_LOGBOOK -> fragment = LogbookFragment()
TAG_RECIPES -> fragment = RecipesFragment()
TAG_PROFILE -> fragment = ProfileFragment()
else -> fragment = MeetingPlacesFragment()
}
}
val transaction = supportFragmentManager.beginTransaction()
if (currentFragment != null) {
Log.i("jacek", "hiding " + currentFragment.javaClass.simpleName)
transaction.hide(currentFragment)
}
if (fragmentExists) {
Log.i("jacek", "showing " + fragment.javaClass.simpleName)
transaction.show(fragment)
} else {
Log.i("jacek", "adding " + fragment.javaClass.simpleName)
transaction.add(R.id.container, fragment, tag)
}
transaction.commit()
}
The fragments are quite heavy. I will try with some lightweight ones, but still that shouldn't be a problem in my opinion. Is there anything else I could try?
I'm using the latest support library - 25.2.0
Also I'm not interested in replacing the fragments as the point is to add crossfade animation without recreating them
You need to reuse the same instance of a fragment that you wanted to hide or show.
private fun replaceFragment(fragment: Fragment) {
supportFragmentManager.beginTransaction().apply {
if (fragment.isAdded) {
show(fragment)
} else {
add(R.id.fmFragmentContainer, fragment)
}
supportFragmentManager.fragments.forEach {
if (it != fragment && it.isAdded) {
hide(it)
}
}
}.commit()
}
#Ali's answer is good, yet imagine if you have 5 fragments. This is another way to show/hide your fragments:
// in BaseFragment
public abstract String getTAG();
//in FragmentA, FragmentB and FragmentC
public String getTAG(){
return TAG;
}
//Activity containing the fragments
//android.support.v4.app.Fragment;
private FragmentA fragmentA; //inherited BaseFragment
private FragmentB fragmentB; //inherited BaseFragment
private FragmentC fragmentC; //inherited BaseFragment
private ConcurrentHashMap<String,BaseFragment> mapOfAddedFragments = new ConcurrentHashMap<>();
/**
* Displays fragment A
*/
private void displayFragmentA() {
displayFragment(fragmentA)
}
/**
* Displays fragment B
*/
private void displayFragmentB() {
displayFragment(fragmentB)
}
/**
* Displays fragment C
*/
private void displayFragmentC() {
displayFragment(fragmentC)
}
/**
* Loads a fragment using show a fragment
* #param fragment
*/
private void displayFragment(BaseFragment fragment){
if(!mapOfAddedFragments.containsKey(fragment.getTAG()))
mapOfAddedFragments.put(fragment.getTAG(), fragment);
showFragment(fragment.getTAG(), R.id.containerBody);
}
/**
* Displays a fragment and hides all the other ones
* #param fragmentTag is the tag of the fragment we want to display
*/
private void showFragment(String fragmentTag, #IdRes int containerViewId){
FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
BaseFragment fragment = null;
fragment = mapOfAddedFragments.get(fragmentTag);
if(fragment != null) {
if (fragment.isAdded())
ft.show(fragment);
else { //fragment needs to be added to the frame container
ft.add(containerViewId, fragment, fragment.getTAG());
}
}
else //the chosen fragment doesn't exist
return;
//we hide the other fragments
for (ConcurrentHashMap.Entry<String, BaseFragment> entry : mapOfAddedFragments.entrySet()){
if(!entry.getKey().equals(fragmentTag)){
BaseFragment fragmentTemp = entry.getValue();
// Hide the other fragments
if(fragmentTemp != null)
if(fragmentTemp.isAdded())
ft.hide(fragmentTemp);
}
}
//commit changes
ft.commit();
}
And to instantiate them you can do this in the onCreate() method of your activity:
//don't forget to get the .TAG elsewhere before using them here
//never call them directly
private void instantiateFragments(Bundle inState) {
if (inState != null) {
fragmentA = inState.containsKey(FragmentA.TAG) ?
(FragmentA) getSupportFragmentManager().getFragment(inState, FragmentA.TAG):
FragmentA.newInstance(FragmentA.TAG,"0");
fragmentB = inState.containsKey(FragmentB.TAG) ?
(FragmentB) getSupportFragmentManager().getFragment(inState, FragmentB.TAG):
FragmentB.newInstance(FragmentB.TAG,"1");
fragmentc = inState.containsKey(FragmentC.TAG) ?
(FragmentC) getSupportFragmentManager().getFragment(inState, FragmentC.TAG):
FragmentC.newInstance(FragmentC.TAG,"2");
}
else{
fragmentA = FragmentA.newInstance(FragmentA.TAG,"0");
fragmentB = FragmentB.newInstance(FragmentB.TAG,"1");
fragmentc = FragmentC.newInstance(FragmentC.TAG,"2");
}
}
Edit according to Shujaat Ali Khan's question:
The BaseFragment extends support4 fragment:
public abstract class BaseFragment extends Fragment {
public abstract String getTAG();
//whatever we can add to be inherited
}
FragmentA for example:
public class FragmentA extends BaseFragment {
// Store instance variables
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
public static final String TAG = "FragmentA";
// newInstance constructor for creating fragment with arguments
public static FragmentA newInstance(String param1, String param2) {
FragmentA fragment = new FragmentA();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
// Store instance variables based on arguments passed
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
// Inflate the view for the fragment based on layout XML
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragmentA, container, false);
return view;
}
//other lifecycle methods
#Override
public String getTAG() {
return TAG;
}
}
Finally the R.id.containerBody is the id of a FrameLayout containing the fragments in the activity containing these fragments.
The problem here is even though you're hiding "current" fragment, there are other fragments loaded in the memory and that gives inconsistent behaviour.
You should be able to fix this by hiding all the fragment except the fragment you want to show.
Thanks to this answer. Show hide fragment in android
eg:
private FragmentA fragmentA;
private FragmentB fragmentB;
private FragmentC fragmentC;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentA = FragmentA.newInstance();
fragmentB = FragmentB.newInstance();
fragmentC = FragmentC.newInstance();
}
protected void displayFragmentA() {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
if (fragmentA.isAdded()) {
ft.show(fragmentA);
} else {
ft.add(R.id.fragement_container, fragmentA);
}
if (fragmentB.isAdded()) { ft.hide(fragmentB); }
if (fragmentC.isAdded()) { ft.hide(fragmentC); }
ft.commit();
}
Similarly you will have to write functions for displayFragmentB() and displayFragmentC()
I created an interface so I can set text on a FragmentB when I press a TextView on FragmentA. Something is not working and I can't figure this out.
I've created an interface called Communicator:
public interface Communicator {
void respond(String data);
}
On FragmentA I've set a reference on the interface called Communcator and an OnClickListener on the TextView:
Communicator comm;
homeTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
comm.respond("Trying to set text on FragmentB from here");
}
});
FragmentB, set my method to change text:
public void setText(final String data) {
startTripTxt.setText(data);
}
Finally in MainActivity I've implemented the interface .. I think here is where I'm doing something wrong:
#Override
public void respond(String data) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.container_main, new FragmentB(), "fragment2").addToBackStack(null).commit();
FragmentB fragmentB= (FragmentB) getSupportFragmentManager().findFragmentByTag("fragment2");
if (fragmentB != null) {
fragmentB.setText(data);
}
}
Fragment 2 loads, but the text is empty.
Fragment 2 loads, but the text is empty.
You implement Communicator is ok but the way you call FragmentB and passing data is not ok. That 's is the reason why you cannot get text from FragmentB. the right way to send data to FragmentB should be like this:
public static FragmentB createInstance(String data) {
FragmentB fragment = new FragmentB();
Bundle bundle = new Bundle();
bundle.putString("data", data);
fragment.setArguments(bundle);
return fragment;
}
And you can get data from FragmentB by:
Bundle bundle = getArguments();
if (bundle != null) {
String data = bundle.getString("data");
}
It looks like after you declare fragmentB, you're meaning to set the text on that fragment. You are Instead calling trainFinderFragment.setText(). Is that your issue?
FragmentB fragmentB= (FragmentB) getSupportFragmentManager().findFragmentByTag("fragment2");
if (fragmentB != null) {
fragmentB.setText(data);
}
I got a situation here .
I have FragmentActivty which holds a Fragment . when i click on a button in Fragment i am going to a Activty . when i reach the activity i do something which will affect the data displayed in Fragment where i came from. So in order to bring that changes in fragment i would like to give a callback from the Activity to Fragment. First i thought of implementing onActivityResult. But i realized it's not what i needed.
Is my approach is wrong?? Please guide me
MyActivity extends FragmentActivity
MyActivity holds
MyFragment extends Fragment
From here i'm going to
SecondActivity extends Activity
from SecondActivity i need to get a callback to MyFragment . Is there anything wrong with my approach ??
EDIT:
public class MainActivity extends FragmentActivity {
private FrameLayout frameLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
frameLayout = (FrameLayout) findViewById(R.id.framelayout);
loadFragment();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void loadFragment() {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
MyFragment myFragment = new MyFragment();
fragmentTransaction.add(R.id.framelayout, myFragment);
fragmentTransaction.commit();
}
}
MyFragment.java
public class MyFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_view, container, false);
Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
callActivity();
}
});
return view;
}
private void callActivity() {
Intent intent = new Intent(getActivity(), SecondActivity.class);
startActivityForResult(intent, 10);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// super.onActivityResult(requestCode, resultCode, data);
Log.e("MyFragment Inside", "Onresultttt");
if (requestCode == 10) {
if (resultCode == Activity.RESULT_OK) {
Log.e("Result code", Activity.RESULT_OK + " okkk");
}
if (resultCode == Activity.RESULT_CANCELED) {
Log.e("Result code", Activity.RESULT_CANCELED + "cancelll inside fragment");
}
}
}
}
SecondActivity.java
public class SecondActivity extends Activity {
private static final String TAG = "second activity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondactivity_view);
}
#Override
public void onBackPressed() {
// super.onBackPressed();
Intent intent = new Intent();
setResult(RESULT_CANCELED, intent);
Log.e(TAG, "result setted");
finish();
}
}
Refer this question it will help you understand what is happening:
onActivityResult is not being called in Fragment
If I understand your situation correctly, then the correct way of handling the communication is to have SecondActivity pass back information to MyActivity, which will in turn configure it's instance of MyFragment directly. You don't want to be accessing Fragments from Activities unless the Fragment is attached to the Activity.
As for how to do the communication, as you suggested, one way of doing it would be through the use of startActivityForResult(). See this answer for more details: How to manage `startActivityForResult` on Android?
Just a note about startActivityForResult(). If you are calling it from the Fragment then your Fragment will receive the result, not your Activity. There are also some other issues with calling startActivityForResult() from a Fragment, so I would generally recommend that you instead call it from the Activity and therefore handle the result from the Activity.
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, INTENT_CODE);
The best practice way of communicating from a Fragment to an Activity is by defining an Interface in the Fragment, which the Activity implements.
public class MyFragment extends Fragment {
public interface MyFragmentListener() {
public void onMyFragmentEvent();
}
public void startTheActivityForResult() {
((MyFragmentListener)getActivity()).onMyFragmentEvent();
}
}
public class MainActivity extends FragmentActivity implements MyFragmentListener{
#Override public void onMyFragmentEvent() {
}
}
The Fragment then simply casts the reference to the Activity it is attached to, knowing that the Activity must implement the Listener, thus allowing you to reuse the Fragment in other Activities.