I know this issue was discussed many times, but no solution works for me. I have an activity
with fragment
attached and using interface
I'm calling a method from my activity
that is replacing this fragment
with another one. I want this method also to call a method from my new 'fragment', but however I implement that the application crashes when reaching this point. Does somebody know a solution for that? I'm new to Android development.
public void replacingMethod(String text){
NewFragment newFragment = new NewFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newFragment).addToBackStack(null).commit();
NewFragment f = (NewFragment) getSupportFragmentManager().findFragmentById(R.id.new_fragment);
f.displayText(text);
}
I've tried to call displayText()
from newFragment
before and after replacement but it didn't work.
Your fragment should be attached to the activity to display text. The right way is to pass the text you want to show to the fragment using it's arguments with Fragment#setArguments(Bundle)
and then get it inside fragment using Fragment#getArguments()
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments