Set progressBar visibility from Fragments? - java

I'd like to know how I can set eg a progressBar visibility from within several Fragments?
class MyActivity extends FragmentActivity {
public setProgressBar(boolean toggle) {
progressBar.setVisibility(toggle);
}
}
Now I want to call setProgressBar(true/false) from within MyActivity, but also from any Fragment classes.

In your Fragment call getActivity(), cast it to MyActivity and call setProgressBar(toggle)
((MyActivity) getActivity()).setProgressBar(toggle);

Related

How to call a method in a fragment through the onClick in another fragment?

I am building an app with 2 fragments. The 1st fragment has an ImageView and 2 TextViews and the 2nd fragment has a set of buttons and EditTexts. In the 2nd fragment, I have a button called "Save". When this button is clicked, I want to download the image inside my 1st fragment to my device folder (The ImageView, the URI, the bitmap and canvas objects are all in the 1st fragment).
Because fragments can't communicate with one another directly, I was thinking of doing this with an interface. What I did was:
I've declared my interface in the 2nd fragment
Applied the logic of the interface's method in the MainActivity (which is the shared activity between the 2 fragments)
Fed the necessary parameters for the method in the 1st fragment.
Didn't work
But I don't think that this was the correct order so it's no surprise that it didn't work. How do I apply the interface in a way that a button click in the 2nd fragment downloads the image in the 1st fragment?
You could try one of these three options:
1.) Using callbacks to communicate via the activity
As shown in this article, you can define an interface in fragment 2 which is then called when the button is clicked. Your activity (which holds fragment 2) provides an implementation for that interface in which the activity calls a method in fragment 1 for downloading the image. For example:
Fragment 1 providing the download method
public class Fragment1 extends Fragment {
OnButtonClickedListener mCallback;
public void startImageDownload() {
// Download the image
}
// ...
}
Fragment 2 defining and calling the interface
public class Fragment2 extends Fragment {
OnButtonClickedListener mCallback;
// Some kind of init method called by onCreate etc.
private void init() {
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Call the listener if present
if(mCallback != null) {
mCallback.onButtonClicked();
}
}
});
}
public void setOnButtonClickedListener(Activity activity) {
mCallback = activity;
}
// Container Activity must implement this interface
public interface OnButtonClickedListener {
public void onButtonClicked();
}
// ...
}
The Activity reacting on the Button click and calling the download method
public static class MainActivity extends Activity implements Fragment2.OnButtonClickedListener {
Fragment1 mFragment1;
#Override
public void onAttachFragment(Fragment fragment) {
if (fragment instanceof Fragment2) {
// Register the listener
Fragment2 fragment2 = (Fragment2) fragment;
fragment2.setOnButtonClickedListener(this);
} else if (fragment instanceof Fragment1) {
// Keep a reference to fragment 1 for calling the "download" method
mFragment1 = (Fragment1) fragment;
}
}
#Override
public void onButtonClicked() {
// Handle the button click
mFragment1.startImageDownload();
}
}
This way you avoid linking the fragments together, instead you have the activity beeing a loose "connection" between fragment 1 and fragment 2.
This is just an exmple, i did not had time to test it. But i hope it helps.
2.) Using a local broadcast
I would recommend using the LocalBroadcastManager for sending a broadcast in fragment 1 (that the button was clicked) and receiving it in fragment 2 (downloading the image). Here is a great article about local broadcasts.
3.) Another option is to use ViewModel
The ViewModel was recently introduced by the new Android Jetpack and "is designed to store and manage UI-related data in a lifecycle conscious way. The ViewModel class allows data to survive configuration changes such as screen rotations." (from ViewModel Overview).
It can also be used to share data between two fragments: Your activity basically holds the ViewModel and the fragments (inside that activity) can get access to it by calling: ViewModelProviders.of(getActivity()).get(SharedViewModel.class);
I think your scenario you could use Observers or some kind of LiveData to react to the button-click via a ViewModel.
Thanks to #Elletlar for helping me improve my answer.

Memory leaks on Listener passed to RecyclerView adapter?

I have a question about possible memory leaks passing a listener to a RecyclerView Adapter. If I implement an interface in my activity (my listener), and I pass this activity as parameter to a RecyclerView adapter, will I have problems with that listener if the activity is destroyed, or will it be destroyed when the activity finish?
Eg::
`
public class MyActivity extends AppCompatActivity implements MyListener {
#overrides
public void onCreate (blablabla) {
MyAdapter adapter = new MyAdapter (this);
mRecyclerView.setAdapter (adapter);
}}
`
Sorry for the code but Im writing with my mobile phone. Can anyone argue if I will have problems with memory or cyclics?
Thank you!

Android declaring a textview in parent class xml and setting listener in child class

Actually, I have 2 activities, BaseActivity and ChildActivity. ChildActivity is extending BaseActivity. I am declaring a TextView in Base class XML and I wanted to set listener of that TextView in ChildActivity.
I can directly call the functions defined in BaseActivity from ChildActivity, but how can I set onClickListener in child class?
For that You can use
interface
create one interface
interface Textview_InterFace
{
Textview findTextview();
}
Second step is to implement to your BaseActivity and also implement method
And in Child Activity
public class childActivity extends BaseActivity
{
Textview_Interface my_textview;
Textview textview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
my_textview = (Textview_Interface) this;
textview = my_textview.findTextview();
//perform here your onClickListner
}

Android Access Fragment Variable

What I am basicaly trying is to access a variable inside a Fragment and get rid of it in my activity.
It worked to get the variable of the activity in my fragment but not the other way around:
what I did:
// get method of MainActivity
final MainActivity activity = (MainActivity) getActivity();
is it even possible to make this "the other way around"?
(Access variable of Fragment in my Activity)?
You need to implement listeners.
You can read more about here:Communicating with Other Fragments
Here is a code example how to pass data (or null) from Activity to a Fragment:
public class FragmentA extends Fragment implements FragmentCommunicator{
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity)getActivity()).fragmentCommunicator = this;
}
#Override
public void passDataToFragment(String str) {
//str is the string variable you pass from the Activity, it can be null...
}
}}
Next the FragmentCommunicator Class:
public interface FragmentCommunicator{
public void passDataToFragment(String str);}
And the Activity:
public class MainActivity extends FragmentActivity{
public FragmentCommunicator fragmentCommunicator;
public void someMethod(String someString) {
fragmentCommunicator.passDataToFragment(someString);
}}
When you call passDataToFragment() from the Activity it will pass the string (or any other variable) to the fragment method passDataToFragment().
You can get your fragment by id/tag from fragment manager and do whatever you want with them.
that's super easy.
if you are adding your fragments in runtime (using FragmentManager)
you are creating objects of that fragment...just keep the reference with you and you are good to call any function or access any variable of the fragment.
e.g. you fragment is MyFragment
MyFragment mf = new MyFragment();
getSupportFragmentManger().beginTrans......you know the code to add a fragment
then simply call any method...for say... change() by mf.change();
or you can do something like
MyFragment mf = (MyFragment) findFragmentById(R.id.container);
and then again mf.change();

Fragmentactivity and onCreate

I've got a problem with fragmentactivity and onCreate method. My classes looks like this:
public abstract class B extends FragmentActivity implements View.OnKeyListener
{}
public class A extends B
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// stuff
}
}
OnCreate in class A is not called. I created layout from another after click and onCreate is in second layout. I don't know where is the problem.
EDIT:
leandrocastelli - i started new layout simply by creating object new A(getActivity()) not by intend.
Arash - you have right. I've to start new Activity. And now startActivity throws nullpointer :/
blackbelt - it was create like above and now i've switch to startActivity.
You extend A class from FragmentActivity so your A class which is a FragmentActivity first should be called someway by startActivity,if you didn't do that so your subclass's onCreate would never get called

Categories