I\'m having a trouble with findViewByid
but I can\'t find where the problem is.
Here\'s my FirstFragment
class code:
import and
getActivity().findViewById()
works. However, this isn't a good practice because the fragment may be reused in another activity.
The recommended way for this is to define an interface.
The interface should contain methods by which the fragment needs to communicate with its parent activity.
public interface MyInterfcae {
void showTextView();
}
Then your activity implements that Interface.
public class MyActivity extends Activity implements MyInterfcae {
@Override
public void showTextView(){
findViewById(R.id.textview).setVisibility(View.VISIBLE);
}
}
In that fragment grab a reference to the interface.
MyInterface mif = (MyInterface) getActivity();
Call a method.
mif.showTextView();
This way, the fragment and the activity are fully decoupled and every activity which implements that fragment, is able to attach that fragment to itself.