Call viewpager's asynctask from above activity - java

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();
}
}

Related

How to change view visibility at parent fragment from child fragment?

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();
}

How to pass OnClickListener from Activity to Fragment

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 */ });

Communicate from Activity to Fragment using Interface

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)'

Passing data from Fragment to it's Container Activity is not working. Any ideas?

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("");

how to communicate values between fragments in android?

I have an android activity with 4 fragments.
In Each fragment, there exists a text box, On the 4th fragment, a button exists.
on clicking the button, all the values(numbers) present in the text-boxes in the different fragment is added together and displayed as a Toast.
Note: Fragments must not communicate with each other.(without use of static variables or public in fragments and access it in the 4th fragment.)
Now, I have fragments but i need to know What communication technique should i have to use in this case?
You could store the Data in the MainActivity and read it from any Fragment like:
//MainActivity
private String mItem = "";
public void getItem(){
return this.mItem;
}
public void addItem(String searchItem){
this.mItem += searchItem;
}
//FRAGMENT 1
public void onButtonClick(){
//adding a string to your item in the MainActivity
((MainActivity)getActivity()).addItem("String from Frag1");
}
//FRAGMENT 4
private void onButtonClick{
//reading the string
String toastMessage = ((MainActivity)getActivity()).getItem();
}
One way i think you could do this is by communicating through the activity by using getActivity() from each fragment since they all share the same activity
public interface IAddItem{ public void addItem(int fragID, String value) };
Create this interface and implement it to your MainActivity:
Public class MainActivity extends Activity implements IAddItem{
private String mValue;
public void addItem(int fragID, String value){
switch(fragID){
case R.id.frag_1:
mValue = value;
break;
case //Do your other code here
}
}
}
In using it in other fragments using this: private IAddItem mCallback;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (IAddItem) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement IAddItem");
}
}
And call it like this: mCallback.addItem(R.id.frag_1, text.getText().toString());
Before calling another fragment just put code below:
Bundle bundle=new Bundle();
bundle.putString("MY_DATA", mStringData);
mFragment.setArguments(bundle);
After commit() mFragment. In second fragment where you want to get this data use:
String data = getArguments().getStringExtra("MY_DATA");
Another way for this in your case, you can use SharedPreference
Save data in preference like:
SharedPreference mPreference=PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
mPreferences.edit().putString("MY_DATA", mStringData).commit();
to get this data in another fragment use:
String data = mPrefrences().getStringExtra("MY_DATA");

Categories