I am using nested fragment that have hierarcy looks like this :
MainActivity>parent fragment> child fragment.
mainactivity hold few parent fragment like fragment10,fragment11,fragment12.
and on parent fragment hold few fragment too like fragmentinti,fragmentdasar,fragmentindikator.
At fragment10 i hv relativelayout that blocking listview, the recyclerview is showing information if i dont hv data.
At fragmentinti i hv process that getting from API if hv data relativelayout on the fragment10 must be setvisibility GONE.
I have trying to use some code at stackoverflow but its still give me an error,
first code that i tested
fragmentKelas10 = (fragmentKelas10) getParentFragment();
fragmentKelas10.getView().findViewById(R.id.nodata).setVisibility(View.GONE);
second
fragmentKelas19 parentFrag = ((fragmentKelas10)fragmentKompetensiInti.this.getParentFragment());
parentFrag.setVisibilityNoData(View.GONE);
third
(fragmentkelas10 getparentfragment()).setvisivity(View.GONE);
and on my parentfragment i add public method
public void setVisibilityNoData(int visibility){
nodata.setVisibility(visibility);
}
*nodata is relativelayout
all above code that i tried return error message like this
Attempt to invoke virtual method 'android.view.View edu.stikom.molearn.fragment.fragmentKelas10.getView()' on a null object reference
Make The activity or fragment you want to return to implement OnFragmentInteractoinLister and when on no data gets called it can return to that fragment or activity and you can hide whatever you want.
private OnFragmentInteractionListener mListener;
public void onNoData() {
if (mListener != null) {
mListener.onFragmentInteraction();
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction();
}
Related
I need to pass an OnClickListener object from activity to a Fragment.
I can't pass argument to Fragment constructor, so i want to do something like:
Bundle bundle = new Bundle();
bundle.putCustomObject(new OnClickListener(){...});
I think the best approach is to create an interface with a method onClick() and set the callback from the Activity to the Fragment.
Interface
public interface FragmentClickListener
{
void onClick();
}
Create a variable and method in Fragment class to receive the FragmentClickListener:
private FragmentClickListener listener;
public void setOnClickListener(FragmentClickListener listener)
{
this.listener = listener;
}
Then in some method of your Fragment class you can call the listener like this:
void someMethod()
{
if(listener != null) {
listener.onClick();
}
}
In your Activity class define the call back like this:
fragment.setOnClickListener(new FragmentClickListener(){
#Override
public void onClick() {
//do something here
}
});
Add a method to the fragment:
public void setOnClickListener(OnClickListener listener) { mListener = listener;}
Call Fragment's method:
mFragment.setOnClickListener(new OnClickListener() { /* YOUR CODE HERE */ });
I have activity and activity has a viewpager.
I want to send my edittext's text to pager 1 and call page1's asynctask from activity has viewpager.
Here is image
in case you want to send text from outside ViewPager (Activity) to somewhat screen inside ViewPager,
try implement this in activity, to provide the way to get your text from this activity through interface
public class TestActivity implement GetTextCallback {
public interface GetTextCallback {
String getText()
}
#Override
public String getText() {
return editText.getText().toString();
}
}
and this in ViewPager's fragment, to get text from your activity through interface you created
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (getTextCallback == null) {
getTextCallback = (GetTextCallback) activity;
}
}
public void whenYouWantToGetText() {
if (getTextCallback != null) {
getTextCallback.getTextYouWant();
}
}
I have searched SO for this problem but was not able to find anything which would solve my problem. My problem is, I have a activity which contains FrameLayout which is constantly updated with different fragments. The top view and bottom view are going to remain same hence they are in the layout of the
activity.
As you can see bottom view has a button on click of that i want to make changes in the fragments which will be present in the FrameLayout.
I have created a interface
public interface ShowFormula {
void showFormula(boolean show);
}
which i will use to implement in the fragment.
Now the main problem in my MainActivity class i am trying to initialize the interface but not able to as i am getting class cast exception
showFormula = (ShowFormula) this;//yes i know this is wrong
How should i initialize this in order to communicate with the fragment.
Main goal is to toggle the view in fragments on click of the button in activity.
Thanks in advance.
You don't need to use an interface to make calls from an Activity to a Fragment. Just keep a reference to the current Fragment, and call into a public method in the Fragment from the Activity.
If you have multiple Fragments and you don't want to keep a reference for each one, you can create a Fragment base class, declare the common method in the base class, and then implement that method override in all of your Fragments that inherit from the base Fragment. Then, keep one reference of the base Fragment type, and always have it set to the Fragment that is shown currently.
Activity ---> Fragment
Communication from Activity to Fragment is pretty straightforward. You
really don't need a listener.
Let's say you have a method inside Fragment share()
public class MyFragment extends Fragment{
public static MyFragment getInstance()
{
return new MyFragment();
}
........
public void share()
{
// do something
}
}
How to call share() method from an Activity?
Get the reference of the Fragment and call the method. Simple!
MyFragment myFragment = MyFragment.getInstance();
myFragment.share();
You can see the full working code for Fragment to Fragment Communication
Just to add to Daniel Nugent's brilliant answer, here are snippets from my working code for delegating calls from Activity to Fragment.
I have a MVP architecture and I have defined the error handling method showError on the BaseView class and the code below demonstrates how to handle the UI on a TargetFragment class. I, specifically needed to hide my progress spinner on the fragment upon any error scenario. Here's the code snippets for the base classes:
public interface BaseView {
void showError(ErrorResponse errorResponse);
}
public abstract class BaseActivity implements BaseView {
#Override
public void showError(ErrorResponse errorResponse) {
// Check error condition or whatever
// ...
MaterialDialog dialog = new MaterialDialog.Builder(this)
.title(R.string.dialog_error_title)
.content(R.string.error_no_internet)
.positiveText(R.string.dialog_action_ok)
.build();
dialog.show();
}
}
public abstract class BaseFragment implements BaseView {
#Override
public void showError(ErrorResponse errorResponse) {
((BaseView) getActivity()).showError(errorResponse);
}
}
And, this is how I handle UI inside my TargetFragment class:
public final class TargetFragment extends BaseFragment implements TargetView {
#Override
public void showError(ErrorResponse errorResponse) {
super.showError(errorResponse);
hideSpinner();
// Do other UI stuff
// ...
}
private void hideSpinner() {
spinner.setVisibility(View.INVISIBLE);
}
}
a clean solution:
public interface ShowFormula {
public void showFormula(boolean show);
}
public class MyActivity implements ShowFormula {
...
#Override
public void showFormula(boolean show) {
/** Your Code **/
}
...
}
public class MyFragment {
private ShowFormula listener;
...
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
listener = (ShowFormula) activity;
// listener.showFormula(show?);
} catch (ClassCastException castException) {
/** The activity does not implement the listener. **/
}
}
...
}
simple thing make public method in fragments then call it on from your activity.
e.g
MyFragment fragment = new MyFragment();
fragment.doSomeThing();
doSomeThing() is a public method in MyFragment.
Activity to Fragment Communication via Interface:
public class MyActivity {
private ShowFormula showFormulaListener;
public interface ShowFormula {
public void showFormula(boolean show);
}
public void setListener(MyFragment myFragment) {
try {
showFormulaListener = myFragment;
} catch(ClassCastException e) {
}
}
}
public class MyFragment implements ShowFormula{
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
((MyActivity) activity).setListener(this);
} catch (ClassCastException e) {
Log.e(TAG, e.toString());
}
}
#Override
public void showFormula(boolean show) {
/** Your Code **/
}
}
Once you are done setting this, you can call 'showFormulaListener.showFormula(boolVal)'
I'm trying to pass variable data back to a Fragment's Containing Activity but it just doesn't seem to be working.
In the fragment I have:
public class MainActivityFragment extends Fragment {
public String profile_id;
OnPassIdListener onPassIdListener;
private FacebookCallback<LoginResult> mCallback = new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
...
}
onPassIdListener.passId(profile_id);
}
public void onAttach(Context context) {
super.onAttach(context);
try {
onPassIdListener = (OnPassIdListener) context;
} catch (Exception ex) {
}
}
public interface OnPassIdListener {
void passId(String id);
}
In the container activity I have:
#Override
public void passId(String id) {
TextView textview = (TextView) findViewById(R.id.prof_id_test);
textview.setText(id);
}
However when the textview.setText is called, it is set to null... meaning that the variable is empty.
Additionally, I have checked that the variable actually contains data BEFORE it is passed to the activity and it does so it must be something to do with the way I am passing it over.
Any ideas? Thanks in advance for any tips.
You can get the attached activity calling it like
ContainerActivity activity = (ContainerActivity) getActivity();
Then just use their methods
activity.passId("");
I have a fragment which is instantiated by an Activity. The issue that I'm having is that I have another class in the fragment which takes as a parameter an Activity context.
public class LocationQueries {
Activity context;
private static int REQUEST_CODE_RECOVER_PLAY_SERVICES = 200;
public LocationQueries(Activity context) {
this.context = context;
}
public boolean checkGooglePlayServices() {
int checkGooglePlayServices = GooglePlayServicesUtil
.isGooglePlayServicesAvailable(context);
if (checkGooglePlayServices != ConnectionResult.SUCCESS) {
/*
* google play services is missing or update is required
* return code could be
* SUCCESS,
* SERVICE_MISSING, SERVICE_VERSION_UPDATE_REQUIRED,
* SERVICE_DISABLED, SERVICE_INVALID.
*/
GooglePlayServicesUtil.getErrorDialog(checkGooglePlayServices,
context, REQUEST_CODE_RECOVER_PLAY_SERVICES).show();
return false;
}
return true;
}
}
I instantiate that class in my fragment like this
private LocationQueries locationQueries = new LocationQueries(getActivity());
but when I try to use locationQueries.checkGooglePlayServices();
I get Caused by: java.lang.NullPointerException: Attempt to invoke virtual method android.content.pm.PackageManager android.content.Context.getPackageManager() on a null object reference.
It looks like the LocationQueries(getActivity()) doesn't actually pass the activity context. How can I solve this ?
Edit: Everything is working if I do the same from an Activity -> LocationQueries(this);
It seems that you initiate LocationQueries at the wrong place. Indeed, I assume this:
private LocationQueries locationQueries = new LocationQueries(getActivity());
is called as a global variable in your Fragment class.
Instead you should keep your variable as global but set it into onCreate() or onResume(), as follows:
private class Frag extends Fragment {
private LocationQueries locationQueries;
...
#Override
public void onCreate(Bundle inState) {
locationQueries = new LocationQueries(getActivity());
}
}
Your Fragment appears not to be attached to an Activity.
I suspect you have instantiated your LocationQueries object before onAttach(Activity activity) has been called, or after onDetach() has been called on your Fragment.
In such a case, calling getActivity() will return null which is what you then pass to your LocationQueries object resulting in the NPE.