I have fragments making API call via Retrofit + RxJava. I subscribe the Subscription in onActivityCreated and dispose at onDestroy. When I switch from FragmentA to FragmentB while FragmentA is still processing the API call, FragmentB appears below the content of FragmentA. It means FragmentA contents remains on top of the screen and there is no control over it.
FragmentA.java
public class FragmentA extends Fragment {
#BindView(R.id.swipe_refresh) SwipeRefreshLayout mSwipeRefresh;
#BindView(R.id.recycler_view) RecyclerView mRecyclerView;
private ItemsAdapter mAdapter;
private CompositeDisposable mCompositeDisposable = new CompositeDisposable();
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_list, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
ButterKnife.bind(this, view);
mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
fetchItems();
}
});
mAdapter = new ItemsAdapter();
mRecyclerView.setAdapter(mAdapter);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mSwipeRefresh.setRefreshing(true);
fetchItems();
}
#Override
public void onDestroy() {
super.onDestroy();
mCompositeDisposable.clear();
}
private void fetchItems() {
RequestInterface requestInterface = Utils.createService();
mCompositeDisposable.add(requestInterface.getItemsForA()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Response>() {
#Override
public void accept(#NonNull Response response) throws Exception {
mAdapter.swapItems(response.getItems());
mSwipeRefresh.setRefreshing(false);
}
}, new Consumer<Throwable>() {
#Override
public void accept(#NonNull Throwable throwable) throws Exception {
Toast.makeText(getActivity(), "Error: " + throwable.getLocalizedMessage(), Toast.LENGTH_LONG).show();
mSwipeRefresh.setRefreshing(false);
}
}));
}
}
FragmentB.java
public class FragmentB extends Fragment {
//Same as `FragmentA` but different API call
}
I have also did some logging and found out that all of the life-cycle methods for FragmentA including onDestroyView gets called.
You need to call following methods on SwipeRefreshLayout to fix this.
swipeRefreshLayout.setRefreshing(false);
swipeRefreshLayout.destroyDrawingCache();
swipeRefreshLayout.clearAnimation();
super.onDestroyView();
}
Related
I want my layout to be refreshed when the app starts and the data should show in and when the user swipes the data should be updated and show new data if new data is available on the server.
The actual problem is that the data is not loading when the app is started. Rather it is happening only on swiping. How can I achieve that on opening the app the data is loaded first and then the data is updated on swiping?
public class FirstFragment extends Fragment {
FragmentFirstBinding binding;
APIInterfaces interfaces;
ArrayList<VideoModels> list = new ArrayList<>();
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
binding = FragmentFirstBinding.inflate(inflater, container, false);
MyAdapter adapter = new MyAdapter(list,getContext());
binding.myRecyclerView.setAdapter(adapter);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),2);
binding.myRecyclerView.setLayoutManager(gridLayoutManager);
interfaces = RetrofitInstance.getRetrofit().create(APIInterfaces.class);
binding.swipeToRefresh.setRefreshing(true);
binding.swipeToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
interfaces.getPosts().enqueue(new Callback<List<VideoModels>>() {
#SuppressLint("NotifyDataSetChanged")
#Override
public void onResponse(#NonNull Call<List<VideoModels>> call, #NonNull Response<List<VideoModels>> response) {
list.clear();
if (response.isSuccessful()) {
assert response.body() != null;
list.addAll(response.body());
adapter.notifyDataSetChanged();
binding.swipeToRefresh.setRefreshing(false);
}
}
#Override
public void onFailure(#NonNull Call<List<VideoModels>> call, #NonNull Throwable t) {
Toast.makeText(getContext(), "Check you Internet", Toast.LENGTH_SHORT).show();
binding.swipeToRefresh.setRefreshing(false);
}
});
}
});
binding.swipeToRefresh.setRefreshing(false);
return binding.getRoot();
}
}
EDIT-1: Added complete code.
Your answer will helpful for me.
You need to call this method in onResume() state
public void getData() {
interfaces.getPosts().enqueue(new Callback<List<VideoModels>>() {
#SuppressLint("NotifyDataSetChanged")
#Override
public void onResponse(#NonNull Call<List<VideoModels>> call, #NonNull Response<List<VideoModels>> response) {
list.clear();
if (response.isSuccessful()) {
assert response.body() != null;
list.addAll(response.body());
adapter.notifyDataSetChanged();
binding.swipeToRefresh.setRefreshing(false);
}
}
#Override
public void onFailure(#NonNull Call<List<VideoModels>> call, #NonNull Throwable t) {
Toast.makeText(getContext(), "Check you Internet", Toast.LENGTH_SHORT).show();
binding.swipeToRefresh.setRefreshing(false);
}
});
}
if you call binding.swipeLayout.isRefreshing = true
in SwipeRefreshLayout source code, mNotify boolean value is false
so cannot do anything in setOnRefreshListener
public void onAnimationEnd(Animation animation) {
if (mRefreshing) {
// Make sure the progress view is fully visible
mProgress.setAlpha(MAX_ALPHA);
mProgress.start();
if (mNotify) {
if (mListener != null) {
mListener.onRefresh();
}
}
mCurrentTargetOffsetTop = mCircleView.getTop();
} else {
reset();
}
}
this is why setOnRefreshListener not invoke
if you want manual invoke it do like this by reflect set mNotify boolean value is true
val swipeClass = SwipeRefreshLayout::class.java
val method = swipeClass.getDeclaredMethod("setRefreshing", Boolean::class.java, Boolean::class.java)
method.isAccessible = true
method.invoke(binding.swipeLayout, true, true)
After finding the solution for 1 day. Finally, I found my solution.
To use SwipeRefreshLayout work in Fragment and want to refresh your data Onswipe. use the below code and modify it as your requirement.
This is a fragment.
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
binding = FragmentFirstBinding.inflate(inflater, container, false);
// swiping on starting of an app.
binding.swipeToRefresh.setRefreshing(true);
getData();
binding.swipeToRefresh.setRefreshing(false);
// when user swipe
binding.swipeToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#SuppressLint("NotifyDataSetChanged")
#Override
public void onRefresh() {
getData();
}
});
return binding.getRoot();
}
public void getData(){
// you have to put all of your onCreate code here to work properly. most of time it throws an error. So, make sure you added code properly.
ArrayList<VideoModels> list = new ArrayList<>();
MyAdapter adapter = new MyAdapter(list,getContext());
GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),2);
binding.myRecyclerView.setAdapter(adapter);
binding.myRecyclerView.setLayoutManager(gridLayoutManager);
binding.swipeToRefresh.setRefreshing(true);
interfaces = RetrofitInstance.getRetrofit().create(APIInterfaces.class);
interfaces.getPosts().enqueue(new Callback<List<VideoModels>>() {
#SuppressLint("NotifyDataSetChanged")
#Override
public void onResponse(#NonNull Call<List<VideoModels>> call, #NonNull Response<List<VideoModels>> response) {
list.clear();
if (response.isSuccessful()) {
assert response.body() != null;
list.addAll(response.body());
adapter.notifyDataSetChanged();
binding.swipeToRefresh.setRefreshing(false);
}
}
#Override
public void onFailure(#NonNull Call<List<VideoModels>> call, #NonNull Throwable t) {
Toast.makeText(getContext(), "Check you Internet", Toast.LENGTH_SHORT).show();
binding.swipeToRefresh.setRefreshing(false);
}
});
}
I'm implementing the autocomplete suggestion code in Google map place api.
We are using OnMapReadyCallback as an implement.
The MapView.getMapAsync(this) function was originally used in onCreateView. But now I'm going to use it in setupAutoCompleteFragment. However, in MapView.getMapAsync(this), it is not compiled due to this. What can be used?
public class googlemaptab extends Fragment implements OnMapReadyCallback {
MapView mapview;
Button kakaobutton;
public static googlemaptab newInstance(){
return new googlemaptab();
}
public googlemaptab() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_googlemaptab, container, false);
kakaobutton = (Button)view.findViewById(R.id.kakaobutton);
mapview = (MapView)view.findViewById(R.id.google_map_view);
setupAutoCompleteFragment();
return view;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
kakaobutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//클릭하면 카카오maptab으로 이동하겠다.
((MainActivity)getActivity()).replaceFragment(kakaomaptab.newInstance());
}
});
}
#Override
public void onStart() {
super.onStart();
mapview.onStart();
}
#Override
public void onResume() {
super.onResume();
mapview.onResume();
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if(mapview != null)
{
mapview.onCreate(savedInstanceState);
}
}
#Override
public void onMapReady(GoogleMap googleMap) {
LatLng SEOUL = new LatLng(37.56, 126.97);
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(SEOUL);
markerOptions.title("서울");
markerOptions.snippet("수도");
googleMap.addMarker(markerOptions);
googleMap.moveCamera(CameraUpdateFactory.newLatLng(SEOUL));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(13));
}
private void setupAutoCompleteFragment() {
PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)getActivity().
getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
#Override
public void onPlaceSelected(Place place) {
mapview.getMapAsync(this);
}
#Override
public void onError(Status status) {
Log.e("Error", status.getStatusMessage());
}
});
}
}
You could do it better this way
private void setupAutoCompleteFragment(OnMapReadyCallback instance) {
PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)getActivity().
getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
#Override
public void onPlaceSelected(Place place) {
mapview.getMapAsync(instance);
}
#Override
public void onError(Status status) {
Log.e("Error", status.getStatusMessage());
}
});
}
And don't forget to update your onCreateView with the new function signature as the following:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_googlemaptab, container, false);
kakaobutton = (Button)view.findViewById(R.id.kakaobutton);
mapview = (MapView)view.findViewById(R.id.google_map_view);
setupAutoCompleteFragment(this);
return view;
}
You can use Classname.this in this case.
For example: if your fragment name is HomeFragment then,
HomeFragment.this
If this still doesn't work then you can override onViewCreated() function and call mapView.getMapAsync(this) inside onViewCreated()
If needed check this link
have you tried with mapview.getMapAsync(getActivity()); instead of mapview.getMapAsync(this);
I created my test project where i code to communicate between two fragments but actully I want to access activity from fragment.
Here is code to connect fragment to fragment, its working absolutely right without any error but now i want to change this code to connect activity from fragment instead of fragment to fragment communication.
So Please change this code to access activities from fragment. I stuck on this issue for than a week.So Guys please resolve this.
here is my mainaactivity:
public class MainActivity extends AppCompatActivity implements FragmentA.FragmentAListener, FragmentB.FragmentBListener {
private FragmentA fragmentA;
private FragmentB fragmentB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentA = new FragmentA();
fragmentB = new FragmentB();
getSupportFragmentManager().beginTransaction()
.replace(R.id.container_a, fragmentA)
.replace(R.id.container_b, fragmentB)
.commit();
}
#Override
public void onInputASent(CharSequence input) {
fragmentB.updateEditText(input);
}
#Override
public void onInputBSent(CharSequence input) {
fragmentA.updateEditText(input);
}
Here is my FragmentA.java:
public class FragmentA extends Fragment {
private FragmentAListener listener;
private EditText editText;
private Button buttonOk;
public interface FragmentAListener {
void onInputASent(CharSequence input);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_a, container, false);
editText = v.findViewById(R.id.edit_text);
buttonOk = v.findViewById(R.id.button_ok);
buttonOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CharSequence input = editText.getText();
listener.onInputASent(input);
}
});
return v;
}
public void updateEditText(CharSequence newText) {
editText.setText(newText);
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof FragmentAListener) {
listener = (FragmentAListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement FragmentAListener");
}
}
#Override
public void onDetach() {
super.onDetach();
listener = null;
}
}
Here is my FragmentB.java:
public class FragmentB extends Fragment {
private FragmentBListener listener;
private EditText editText;
private Button buttonOk;
public interface FragmentBListener {
void onInputBSent(CharSequence input);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_b, container, false);
editText = v.findViewById(R.id.edit_text);
buttonOk = v.findViewById(R.id.button_ok);
buttonOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CharSequence input = editText.getText();
listener.onInputBSent(input);
}
});
return v;
}
public void updateEditText(CharSequence newText) {
editText.setText(newText);
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof FragmentBListener) {
listener = (FragmentBListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement FragmentBListener");
}
}
#Override
public void onDetach() {
super.onDetach();
listener = null;
}
}
Here is my Fertilizers.java file which i want to access from FragmentA.:
public class Fertilizers extends AppCompatActivity {
RecyclerView mRecyclerView;
List<FertilizerData> myFertilizersList;
FertilizerData mFertilizersData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fertilizers);
mRecyclerView = (RecyclerView)findViewById(R.id.recyclerView);
GridLayoutManager gridLayoutManager;
gridLayoutManager = new GridLayoutManager(Fertilizers.this, 1);
mRecyclerView.setLayoutManager(gridLayoutManager);
myFertilizersList = new ArrayList<>();
mFertilizersData = new FertilizerData("Urea Fertilizer","Urea is a concent","Rs.1900",R.drawable.urea);
myFertilizersList.add(mFertilizersData);
myFertilizersList.add(mFertilizersData); }
}
please write here a block of code to call Fertilzers Activity from FragmentA, I,ll be very thankful to you.
Calling getActivity() in your fragment gives you the calling activity so if MainActivity started your fragment then you would do
(MainActivity(getActivity())).something_from_your_main_activity
Solution found by itself regarding this issue.
FragmentHome.java class should look like this:
public class FragmentHome extends Fragment {
private Button button;
public FragmentHome(){
}
public interface OnMessageReadListener
{
public void onMessageRead(String message);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_home, container, false);
button = (Button)v.findViewById(R.id.bn);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), Fertilizers.class);
intent.putExtra("some"," some data");
startActivity(intent);
}
});
return v;
}
}
FertilizersActivity.java should look like this:
public class Fertilizers extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fertilizers);
Bundle bundle = getIntent().getExtras();
if (bundle != null){
if(bundle.getStringArrayList("some") !=null){
Toast.makeText(getApplicationContext(),"data:" + bundle.getStringArrayList("some"),Toast.LENGTH_LONG).show();
}
}
}
}
Could guys hep me integrate Startapp network in this activity, this is my code it has not the oncreate method i tried to integrate it but i failed. Please help me. You can find below the code and it does not contain the oncreate method.Im new to coding and i tried lot of time to solve this problem it's easy for me if the oncreate method is there i can integrate the ad network easy. Pleas guys any idea to deal with will help me. Thank you
public class MainFragment extends Fragment {
public MainFragment() {
// Required empty public constructor
}
private final String TAG = "MainFragment";
Activity activity;
AdView bannerAdView;
boolean isAdLoaded;
CardView cardVideoToGIF, cardImagesToGIF, cardCaptureImage, cardVideoToAudio, cardVideoCutter, cardGallery;
LinearLayout linearRow2;
private String SELECTED_TYPE = Constants.TYPE_GIF;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_main, container, false);
}
#Override
public void onResume() {
super.onResume();
if (bannerAdView != null) {
bannerAdView.resume();
}
((MainActivity) activity).setTitle("");
((MainActivity) activity).setDrawerState(true);
if (!MyApplication.isFFmpegSupports) {
linearRow2.setVisibility(View.GONE);
}
}
#Override
public void onPause() {
if (bannerAdView != null) {
bannerAdView.pause();
}
super.onPause();
}
#Override
public void onDestroy() {
if (bannerAdView != null) {
bannerAdView.destroy();
}
super.onDestroy();
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initViews(view);
cardVideoToGIF.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showPopupMenu(cardVideoToGIF);
SELECTED_TYPE = Constants.TYPE_GIF;
}
});
Replace your code with the following
public class MainFragment extends Fragment {
// Add these lines of code which is the onCreate method of your Fragment
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// put your integration code here
Log.i("MainFragment", "onCreate()");
}
public MainFragment() {
// Required empty public constructor
}
private final String TAG = "MainFragment";
Activity activity;
AdView bannerAdView;
boolean isAdLoaded;
CardView cardVideoToGIF, cardImagesToGIF, cardCaptureImage, cardVideoToAudio, cardVideoCutter, cardGallery;
LinearLayout linearRow2;
private String SELECTED_TYPE = Constants.TYPE_GIF;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_main, container, false);
}
#Override
public void onResume() {
super.onResume();
if (bannerAdView != null) {
bannerAdView.resume();
}
((MainActivity) activity).setTitle("");
((MainActivity) activity).setDrawerState(true);
if (!MyApplication.isFFmpegSupports) {
linearRow2.setVisibility(View.GONE);
}
}
#Override
public void onPause() {
if (bannerAdView != null) {
bannerAdView.pause();
}
super.onPause();
}
#Override
public void onDestroy() {
if (bannerAdView != null) {
bannerAdView.destroy();
}
super.onDestroy();
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initViews(view);
cardVideoToGIF.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showPopupMenu(cardVideoToGIF);
SELECTED_TYPE = Constants.TYPE_GIF;
}
});
I am new in android and working on app.help me in this.My Log is here
Could not dispatch event: class com.shalom.shalombase.event.RequestPurchaseEvent to handler [EventHandler public void com.shalom.shalombase.activity.MainActivity.onRequestPurchaseEvent(com.shalom.shalombase.event.RequestPurchaseEvent)]: null
at com.squareup.otto.Bus.throwRuntimeException(Bus.java:456)
at com.squareup.otto.Bus.dispatch(Bus.java:386)
at com.squareup.otto.Bus.dispatchQueuedEvents(Bus.java:367)
at com.squareup.otto.Bus.post(Bus.java:336)
Myfragment subcriptionfragment that is posting event to main activity.
public class SubscribeFragment extends ShalomBaseFragment {
private Button subscribeLayout;
private LinearLayout subscribebuttonslayout;
private FrameLayout sampleframe;
private RelativeLayout subscribelayout;
View progressLayout;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.subscribe_layout, container, false);
}
#Subscribe
public void onStoreProgressEvent (StoreProgressEvent event) {
if(event!=null&&progressLayout!=null)
{
if(event.isInProgress)
progressLayout.setVisibility(View.VISIBLE);
else
progressLayout.setVisibility(View.GONE);
}
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initialize(view);
subscribebuttonslayout.setVisibility(View.VISIBLE);
subscribelayout.setVisibility(View.VISIBLE);
subscribeLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
bus.post(new RequestPurchaseEvent(BaseConstants.YEARLY_SUBSCRIPTION));
}
});
getChildFragmentManager().beginTransaction()
.replace(R.id.sample_frame, new LibrarySamleIssueFragment())
.commit();
}
private void initialize(View view) {
subscribeLayout = (Button) view.findViewById(R.id.button1);
subscribebuttonslayout = (LinearLayout) view.findViewById(R.id.subscribe_buttons_layout);
sampleframe = (FrameLayout) view.findViewById(R.id.sample_frame);
progressLayout = (View) view.findViewById(R.id.progress_layout);
subscribelayout = (RelativeLayout) view.findViewById(R.id.subscribe_layout);
}
}
Please help me in this.It is working fine on S3, but it is creating issue on Nexus7.
I dont what is the problem with code.I am confused in this
otto code :
method.invoke(target, event);
the next operation is:
#Subscribe
public void yourMethod(Event event) {
//your code throw exception
}
your Method throw exception catched by otto
so, this is my condition.