How to update view in Fragment from child fragment in android - android

Have two fragments A and B, Fragment A has Textview and Fragment B has edittext and button.
Click on submit in FragmentB need to update textview in FragmentA with Edittext text.
How to do communication between fragment?

n this example, FragmentA call notify.
INotifier
public interface INotifier {
public void notify(Object data);
}
Utils
public class Utils {
public static INotifier notifier;
}
FragmentA
public FragmentA extends Fragment {
public void onCreateView(...) {
}
public void inSomeMethod() {
if (Utils.notifier != null) {
Utils.notifier.notify(data);
}
}
}
FragmentB
public FragmentB extends Fragment implements INotifier {
public void onCreateView(...) {
Utils.notifier = this;
}
#Override
public void notify(Object data) {
// handle data
}
}

You need to interact the activity first which will interact the second fragment. and also read this article on how to do it.
https://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity

The communication between Fragments is done usinng Listeners. When you want to update fragment, use the listener to tell the MainActivity to update the second fragment as recommended by Google http://developer.android.com/training/basics/fragments/communicating.html. Create the interface in Fragment and Implement this in Activity
Listener in Fragment
public interface FragmentUpdateInterface {
void updateFragment(String newText);
}
#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 = (FragmentUpdateInterface ) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement FragmentUpdateListener");
}
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Send the event to the host activity
mCallback.updateFragment("New Text");
}
MainActivity
Implement fragment in MainActivity as
public static class MainActivity extends Activity
implements MyFragment.FragmentUpdateListener{
public void updateFragment(String newText) {
OtherFragment otherFrag = (OtherFragment)
getSupportFragmentManager().findFragmentById(R.id.other_fragment);
if (otherFrag != null) {
otherFrag.updateFragment(newText);
} else {
// Otherwise, we're in the one-pane layout and must swap frags...
// Create fragment and give it an argument for the selected article
OtherFragment otherFrag = new OtherFragment();
Bundle args = new Bundle();
args.putInt(ArticleFragment.ARG_POSITION, position);
otherFrag.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, otherFrag);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
}
Hope this helps.
UPDATE:
You can also use LocalBroadcastManager.getInstance().sendBroadcast() to notify to the other fragment as well.

Related

How to Synchronize Data Between Viewpager Fragments

With the following code I can click a button to send a string from one fragment (Fragment A) into a MainActivity. How would I retrieve the string from the MainActivity and display it on a fragment B all in one go? I would like the fragments to act synchronized; to have Fragment B update itself as soon as I click the button in Fragment A. I can't seem to find anything in SO on this.
Interface:
public interface OnDataListener {
public void onDataReceived(String data);
}
Fragment A data listener:
OnDataListener mOnDataListener;
#Override
public void onAttach(Context context) {
try{
mOnDataListener = (OnDataListener) context;
Log.d(TAG, "onAttach was called" + context);
}catch (ClassCastException e){
Log.e(TAG, "onAttach: ClassCastException: " + e.getMessage() );
}
super.onAttach(context);
}
Button logic in Fragment A's onCreateView:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String newTextString = editTextView.getText().toString();
mOnDataListener.onDataReceived(newTextString);
}
});
MainActivity data receiver
public class MainActivity extends AppCompatActivity implements OnDataListener {
#Override
public void onDataReceived(String data) {
Log.e(TAG, "MainActivity received this string " + data );
}
}
Solution 1: Using Callbacks
//this will work if your fragment instance is active so check viewpager offscreenpage limit
(You can also use solution 3 mentioned by Farid Haq, which do the same by doing work of activity itself)
Here, Activity implements callback OnDataUpdateListener
interface OnDataUpdateListener{
void passDataToFragmentB(String data)
}
Activity code:
Fragment instanceFragmentB;
// intialise it somewhere in your activity
// or get from viewpager adapter
#Override
void passDataToFragmentB(String data){
instanceFragmentB.onDataChange(data)
}
Fragment A code:
OnDataUpdateListener mOnDataUpdateListener;
onAttach(Activity activity){
mOnDataUpdateListener= (OnDataUpdateListener) activity
}
onViewCreated(){
somebutton.onClick{
mOnDataUpdateListener.passDataToFragmentB("someString")
}
}
Fragment B code:
onDataChange(String data){
//do your work with update data passed from fragment A
}
Solution 2: Using EventBus or RxBus
//this will work if your fragment instance is active so check viewpager offscreenpage limit
Using event or rxbus, post new updated value on bus and make destination fragment observing that same value type.
Fragment A:
onViewCreated(){
somebutton.onClick{
EventBus.post("your data")
}
}
Fragment B:
#Subsribe
void onDataChange(String data){
//do your work with update data passed from fragment A
}
Solution 3: Using ViewModel.
Take viewmodel with context of activity in all fragment and Wrap String with LiveData.
Whenever, string data is changed, just post new data on String Livedata object.
All fragment observing that livedata will get updated value.
Solution 1
In your MainActivity
#Override
public void onDataReceived(String data) {
Log.e(TAG, "MainActivity received this string " + data );
Bundle bundle = new Bundle();
bundle.putString("edttext", data);
FragmentB fragment = new FragmentB();
fragment.setArguments(bundle);
// Fragment Transaction method goes here
}
In your FragmentB Retrieve Data
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext = getArguments().getString("edttext");
return inflater.inflate(R.layout.fragment, container, false);
}
Solution 2
You can put callback data to your global variable and use it at fragment transaction at the time of onClickListerner.
Solution 3
A) Create method in your FragmentB
public void changeText (String data) {
textview.setText(data)
}
B) And Pass Value From MainActivity
#Override
public void onDataReceived (String data) {
myInterface.CallBack("Callbacked when onCreate method Created" + data);
Log.d("Tiggered", "respond: ");
FragmentManager manager = getFragmentManager();
FragmentB fragmentB = (FragmentB) manager.findFragmentById(R.id.id_fragmentB);
fragmentB.changeText(data);
}

FragmentTransaction within Fragment

im a new guy on this programming world so i guess this question is simple. So I have one imagebutton on my fragment and I whatI want is that when I click on it, it does a fragment transaction to another fragment; but the thing is that when I run the app and click on the imagebutton, it only "superimposes" the content of the other fragment into the one where the imagebutton is, of course what i want to do is just to go to the other fragment, ive been dealing with this for a while so I hope someone helps me, thanks.
Here is my java code of the fragment
public class inicio extends Fragment {
public inicio() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_inicio,container,false);
ImageButton luces = (ImageButton)view.findViewById(R.id.imageButton);
luces.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
interior interior= new interior();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.inicioo, interior).commit();
}
});
}
}
New code added...
public class transaction extends MainActivity
implements com.example.administradora.prueba.inicio.OnSelectedListener{
public void onButtonSelected() {
interior interior= new interior();
getSupportFragmentManager().beginTransaction().replace(R.id.inicioo, interior).commit();
}
}
but i get this error in the logcat:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.administradora.prueba/com.example.administradora.prueba.MainActivity}: java.lang.ClassCastException: com.example.administradora.prueba.MainActivity#20f8fe9b must implement OnSelectedListener
You shouldn't replace a Fragment from another Fragment. Try to do that through the Activity.
Define some interface for the Fragment to hold
public class MyFragment extends Fragment {
OnSelectedListener mCallback;
// Container Activity must implement this interface
public interface OnSelectedListener {
public void onButtonSelected();
}
#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 = (OnSelectedListener ) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnSelectedListener");
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_inicio,container,false);
ImageButton luces = (ImageButton)view.findViewById(R.id.imageButton);
luces.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Send the event to the host activity
if (mCallback != null) mCallback.onButtonSelected();
}
});
return view;
}
}
And swap the Fragment container in the interface implementation from the Activity.
public class MainActivity extends Activity
implements MyFragment.OnSelectedListener{
...
public void onButtonSelected() {
interior interior= new interior();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.inicioo, interior)
.commit();
}
}
The first two sections of Communicating with Other Fragments is what you are looking for.

Update Activity on closing Fragment

I have an activity A. Once A has been loaded, on clicking a button in A adds a fragment F on A's home Layout. Now once inside F, if i am callinggetActivity().getSupportFragmentManager().popBackStack();, which infacts navigates again into the Activity A. Is there any way I can actually make some changes in the activity A once the fragment has been popbackstacked? I don't know which method is called in the activity once the fragment has been popbackstacked and you have your activity. Please help.
You can add OnBackStackChangedListener(). Just do something like this:
getSupportFragmentManager().addOnBackStackChangedListener(new OnBackStackChangedListener()
{
public void onBackStackChanged()
{
}
});
This callback will be called after each change on the back stack.
create and implement an interface in your activity
public class MyFragment extends Fragment {
OnFragmentCloseListener mCallback;
// Container Activity must implement this interface
public interface OnFragmentCloseListener {
public void onClosed(Bundle data);
}
//call this while closing the fragment
Bundle data = new Bundle();
data.putExtra("key"," value");
mCallback.onClosed(data);
#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 = (OnFragmentCloseListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentCloseListener ");
}
}
}
In Your activity
public static class MainActivity extends Activity
implements MyFragment.OnFragmentCloseListener {
public void onClosed(Bundle data) {
//get the budle data here
}
}

Best approach to show network error in android in Android with Tap to Retry option

I have Navigation Drawer in my application with several Fragments and few new Activity also apart from main application flow.
Current Functionality For navigating to each Fragment, Network is required and in case of any network error, I used to show Dialog. User needs to click on "OK" button and again go back to navigation drawer to retry.
New Approach I am trying User should be shown and error screen similar to LinkedIn android app with option to retry.
As inner Fragments and handling call back can be cumbersome, how to handle this situation efficiently? For individual Activities this can be achieved easily, but worried about the Navigation Drawer and inner Fragments.
Any suggestions?
Make this error layout hidden in this fragment. When there is any network error then change its visibility to VISIBLE. and in this hidden layout add a button to recall same method to check network connection etc.
Let say you have fragment xml like -
fragment -
Relative Layout consisting -
1. -All layouts (VISIBLE) &
2. -Hidden network error layout with a button (GONE)
When there is network error then change 1. 's visibility to - GONE
and 2.'s visibility to VISIBLE
and on retry button call -
checkNetworkConnectionCall();
I hope this will solve you problem.
You can place some ContentFragment in a FrameLayout, then replace with a NetworkDisconnectedFragment when the network disconnects. This would require the button to a call the callback, then when reconnected, replace the NetworkDisconnectedFragment with the old ContentFragment in the callback implementation.
You can include this UI in each fragment and create a BaseFragment which will be extended by every fragment in nav drawer.
Write a method in that base fragment that will do the need full logic of changing the UI.
And whenever you detect a network failure just blindly call that method from base fragment there.
It's been almost 3 years, but I think it may be helpful for somebody. This example uses MVP pattern. BaseNetContentActivity, BaseNetContentFragment and NetworkErrorFragment are encapsulate change UI logic (by fragments' swapping), in case of network error. They should be extended by other classes.
1) BaseNetContentView.java - base interface for all Views, that should show "network error" UI.
public interface BaseNetContentView {
public void showNetworkContentError();
}
2) BaseNetContentFragment.java - base for all Fragments, that should show "network error" UI. It contains listener and corresponding interface.
public abstract class BaseNetContentFragment extends Fragment implements BaseNetContentView {
#Nullable
private OnNetworkErrorListener mOnNetworkErrorListener;
protected final void tryToShowNetworkError() {
if (mOnNetworkErrorListener != null) {
mOnNetworkErrorListener.onNetworkError();
}
}
protected final boolean hasOnNetworkErrorListener() {
return mOnNetworkErrorListener != null;
}
public final void setOnNetworkErrorListener(
#Nullable OnNetworkErrorListener onNetworkErrorListener) {
mOnNetworkErrorListener = onNetworkErrorListener;
}
public interface OnNetworkErrorListener {
public void onNetworkError();
}
}
3) BaseNetContentActivity - base Activity, that handling network error by changing UI fragments
public abstract class BaseNetContentActivity<T extends BaseNetContentFragment>
extends AppCompatActivity implements BaseNetContentFragment.OnNetworkErrorListener {
private static final String TAG = "BaseNetContentActivity";
#Override
public void onNetworkError() {
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment fragment = getCurrentContentFragment(fragmentManager);
// Skip if already NetworkErrorFragment
if (!(fragment instanceof NetworkErrorFragment)) {
setFragmentToActivity(fragmentManager, new NetworkErrorFragment());
}
}
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResId());
Fragment fragment = getCurrentContentFragment(getSupportFragmentManager());
// NetworkErrorFragment is self-sufficient
if (fragment instanceof NetworkErrorFragment) {
return;
}
setNetworkContentFragmentToActivity(savedInstanceState);
}
#Override
public void onAttachFragment(Fragment fragment) {
// Set appropriate listener to fragment
if (fragment instanceof NetworkErrorFragment) {
((NetworkErrorFragment) fragment)
.setOnReloadContentListener(new NetworkErrorFragment.OnReloadContentListener() {
#Override
public void onReloadContent() {
setNetworkContentFragmentToActivity(null);
}
});
} else if (fragment instanceof BaseNetContentFragment) {
((BaseNetContentFragment) fragment).setOnNetworkErrorListener(this);
}
// Don't do anything with other fragment's type
}
#NonNull
protected abstract T createNetworkContentFragment();
protected abstract void setPresenter(#NonNull T fragment, #Nullable Bundle savedInstanceState);
#LayoutRes
protected int getLayoutResId() {
return R.layout.basenetworkcontent_act;
}
#IdRes
protected int getContentFrameId() {
return R.id.network_content_frame;
}
private void setNetworkContentFragmentToActivity(#Nullable Bundle savedInstanceState) {
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment fragment = getCurrentContentFragment(fragmentManager);
if (fragment == null || fragment instanceof NetworkErrorFragment) {
fragment = createNetworkContentFragment();
}
try {
setPresenter((T) fragment, savedInstanceState);
} catch (ClassCastException e) {
// Unexpected fragment type
Log.d(TAG,"Can't set Presenter because of wrong View type (wrong fragment)" + e);
// Casting to T type is safe, because createNetworkFragment() returns T type
fragment = createNetworkContentFragment(); // returns type T
setPresenter((T) fragment, savedInstanceState);
}
setFragmentToActivity(fragmentManager, fragment);
}
private Fragment getCurrentContentFragment(#NonNull FragmentManager fragmentManager) {
return fragmentManager.findFragmentById(getContentFrameId());
}
private void setFragmentToActivity(#NonNull FragmentManager fragmentManager,
#NonNull Fragment fragment) {
fragmentManager.beginTransaction()
.replace(getContentFrameId(), fragment)
.commit();
}
}
4) NetworkErrorFragment
public static class NetworkErrorFragment extends Fragment implements View.OnClickListener {
#Nullable
private OnReloadContentListener mOnReloadContentListener;
private Button mReloadButton;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater,
#Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.networkerror_frag, container, false);
mReloadButton = (Button) root.findViewById(R.id.reload_content_button);
if (mOnReloadContentListener != null) {
mReloadButton.setOnClickListener(this);
} else {
mReloadButton.setVisibility(View.INVISIBLE);
}
return root;
}
#Override
public void onClick(View v) {
if (mOnReloadContentListener != null) {
mOnReloadContentListener.onReloadContent();
}
}
public void setOnReloadContentListener(#Nullable OnReloadContentListener onReloadContentListener) {
mOnReloadContentListener = onReloadContentListener;
}
public interface OnReloadContentListener {
public void onReloadContent();
}
}
Full example at https://github.com/relativizt/android-network-error-ui

Three Fragments, interface on second Fragment requiring implementation on both Main and Second Activity

Taking the default 'Master/Detail' flow template in Eclipse, and adding a third Fragment (let's call it Edit, launched from the pre-existing Detail Fragment) and I'm now looking to open the Edit Fragment when a user clicks on an item in the Detail Fragment.
I've implemented an interface on the Detail fragment, however depending on whether the application is on a Tablet or Phone (dual-pane or not), the Iterface requires to be implemented by either the Detail Activity, or the Main Activity in order to function. I assume this is due to the way that the template implements opening of the Detail Fragment as an activity when the device is not in dual-pane mode.
Have I implemented this incorrectly, or is there a best practice that would allow me to unify the implementation of the interfaces into the main activity?
Here are some reduced snippets from the Master and Detail Fragments, showing the requirement for dual-implementation of the Detail Fragments interface.
Code for WaveListWactivity.java (first Fragment)
public class WaveListActivity extends FragmentActivity implements
WaveListFragment.Callbacks,WaveDetailFragment.Callbacks {
private boolean mTwoPane;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wave_list);
if (findViewById(R.id.wave_detail_container) != null) {
mTwoPane = true;
((WaveListFragment) getSupportFragmentManager().findFragmentById(
R.id.wave_list)).setActivateOnItemClick(true);
}
}
//Interface from WaveListFragment
#Override
public void onWaveSelected(int id) {
if (mTwoPane) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.wave_detail_container, new WaveDetailFragment()).commit();
} else {
Intent detailIntent = new Intent(this, WaveDetailActivity.class);
startActivity(detailIntent);
}
}
//Interface from WaveDetailFragment
#Override
public void onItemSelected(int id) {
if (mTwoPane) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.wave_detail_container, new WaveEditFragment()).addToBackStack(null).commit();
} else {
Intent detailIntent = new Intent(this, WaveDetailActivity.class);
startActivity(detailIntent);
}
}
}
Code for WaveDetailActivity.java (second Fragment)
public class WaveDetailActivity extends FragmentActivity implements WaveDetailFragment.Callbacks {
private boolean mTwoPane;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wave_detail);
if (findViewById(R.id.wave_detail_container) != null) {
mTwoPane = true;
}
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.wave_detail_container, new WaveDetailFragment()).commit();
}
}
//Callback from WaveDetailFragment
#Override
public void onItemSelected(int id) {
if (mTwoPane) {
getSupportFragmentManager().beginTransaction().replace(R.id.wave_detail_container, new WaveEditFragment()).addToBackStack(null).commit();
} else {
Intent detailIntent = new Intent(this, WaveEditActivity.class);
startActivity(detailIntent);
}
}
}
I think you may be slightly confused as to the difference between FragmentActivity, Fragments and the callback interfaces that you need to implement on your Activity. From the looks of it, all the code snippets are Activity classes and not Fragments. I would expect a Fragment to look something like:
/** From http://developer.android.com/training/basics/fragments/communicating.html */
public class HeadlinesFragment extends ListFragment {
OnHeadlineSelectedListener mCallback;
// Container Activity must implement this interface
public interface OnHeadlineSelectedListener {
public void onArticleSelected(int position);
}
#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 = (OnHeadlineSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
...
}
And then your Activity:
public static class MainActivity extends Activity
implements HeadlinesFragment.OnHeadlineSelectedListener{
...
public void onArticleSelected(int position) {
// The user selected the headline of an article from the HeadlinesFragment
// Do something here to display that article
}
}
The overall 'flow' of this should be:
Fragments act as blobs of UI code with callbacks for all the interesting stuff.
Callbacks implemented by Activity.
The overall number of actions you can do depends on the fragments which depends on the size of your screen (so you may have 2 fragments which feed into a single Activity). The logic for determining whether to show one or two fragments should be done in the Activity.

Categories

Resources