0
I am developing an app and am using 3 Fragments in a slider layout. And when I slide from the first tab to the third tab, my first tab calls the onStop method and consequently, everything in that tab is destroyed.
Is there any way to stop the onStop method? I wish he wouldn’t be called.
Fragment:
/**
* Created by Lucas on 01/10/2017.
*/
public class ProdutosFragment extends Fragment {
private FirebaseDatabase firebaseDatabase;
private DatabaseReference databaseReference;
Button Filtrar;
List<String> areas = new ArrayList<String>();
RecyclerView recyclerView;
RecyclerAdapter adapter;
int i=0;
RecyclerView.LayoutManager layoutManager;
ArrayList<Produtos>arrayList=new ArrayList<>();
SearchView searchView;
ProgressDialog progressDialogFiltrar;
int conta=0;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.produtosfragment,container,false);
String NomeSupermercado = Singleton.getInstance(getApplicationContext()).getNomeSupermercado();
FirebaseProdutos firebaseProdutos= new FirebaseProdutos();
FirebaseAutenticarSupermercado FirebaseAutenticarSupermercado = new FirebaseAutenticarSupermercado();
FirebaseAutenticarSupermercado.Autenticar();
firebaseDatabase = FirebaseDatabase.getInstance();
databaseReference = firebaseDatabase.getReference().child("Estabelecimentos/Estabelecimentos/Supermercados/"+NomeSupermercado+"/produtos/");
Filtrar = (Button)view.findViewById(R.id.btfiltrar);
recyclerView=(RecyclerView)view.findViewById(R.id.recyclerview_id);
layoutManager=new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
searchView=(SearchView)view.findViewById(R.id.search);
searchView.setSelected(false);
searchView.clearFocus();
searchView.setQueryHint("Cerveja, Morango, Aymoré, Bebidas etc");
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
closeKeyboard();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String newText) {
// filter listview
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
newText= newText.toLowerCase();
ArrayList<Produtos> newList= new ArrayList<>();
for(Produtos produtos: arrayList){
String name=produtos.getProduto().toLowerCase();
String marca=produtos.getMarca().toLowerCase();
// String caracteristica=produtos.getCaracteristica().toLowerCase();
String categoria=produtos.getCategoria().toLowerCase();
if(name.contains(newText)||marca.contains(newText)||categoria.contains(newText)){
newList.add(produtos);
}
}
adapter.setFilter(newList);
return true;
}
});
if(adapter==null||arrayList==null||arrayList.size()<1||recyclerView==null){
ProgressDialog prog= new ProgressDialog(getActivity());//Assuming that you are using fragments.
prog.setTitle("Aguarde");
prog.setMessage("Carregando os produtos");
prog.setCancelable(false);
prog.setIndeterminate(true);
prog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
prog.show();
databaseReference.orderByChild("produto").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
arrayList.clear();
adapter=null;
recyclerView.setAdapter(null);
areas.add(" "+"Categoria");
for (DataSnapshot objSnapshot : dataSnapshot.getChildren()) {
Produtos Listar= new Produtos();
Listar = objSnapshot.getValue(Produtos.class);
arrayList.add(Listar);
areas.add(Listar.getCategoria());
}
adapter=new RecyclerAdapter(getActivity(),arrayList);
recyclerView.setAdapter(adapter);
prog.dismiss();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Filtrar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getLayoutInflater();
searchView.setQuery("",false);
closeKeyboard();
//Cria a view a ser utilizada no dialog
ViewGroup view = (ViewGroup) inflater.inflate(R.layout.filtro, null);
//Obtém uma referencia ao hinner
final Spinner SpinnerCategoria = (Spinner) view.findViewById(R.id.spinnercategoria);
//final Spinner SpinnerEspecificos = (Spinner) view.findViewById(R.id.spinnerprodutosespecificos);
Button Limpar = (Button)view.findViewById(R.id.btlimparfiltro);
Button Filtro = (Button)view.findViewById(R.id.btfiltrarproduto);
builder.setView(view);
final AlertDialog builderx=builder.show();
Filtro.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String Categoria = SpinnerCategoria.getSelectedItem().toString();
Log.d("Categoria","Categoria Escolhida"+Categoria);
if(Categoria.equals(" "+"Categoria")){
builderx.dismiss();
}
else {
ProgressDialog proga= new ProgressDialog(getActivity());//Assuming that you are using fragments.
proga.setTitle("Aguarde");
proga.setMessage("Estamos filtrando os produtos para você :)");
proga.setCancelable(false);
proga.setIndeterminate(true);
proga.setProgressStyle(ProgressDialog.STYLE_SPINNER);
proga.show();
databaseReference.orderByChild("categoria").equalTo(Categoria).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
arrayList.clear();
adapter=null;
recyclerView.setAdapter(null);
for (DataSnapshot objSnapshot : dataSnapshot.getChildren()) {
Produtos Listar;
Listar = objSnapshot.getValue(Produtos.class);
arrayList.add(Listar);
i++;
Log.d("Quantidade de produtos", "Quantidade de produtos"+i);
}
adapter=new RecyclerAdapter(getActivity(),arrayList);
recyclerView.setAdapter(adapter);
proga.dismiss();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
builderx.dismiss();
}
}
});
Limpar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Limpar();
}
});
HashSet<String> hashSet = new HashSet<String>();
hashSet.addAll(areas);
areas.clear();
areas.addAll(hashSet);
//Alphabetic sorting.
Collections.sort(areas);
final ArrayAdapter<String> categoriaAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, areas);
categoriaAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
SpinnerCategoria.setAdapter(categoriaAdapter);
}
});
return view;
}
@Override
public void onResume() {
super.onResume();
Log.e("Ciclo", "Activity: Metodo onResume() chamado");
}
@Override
public void onPause() {
super.onPause();
Log.e("Ciclo", "Activity: Metodo onPause() chamado");
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.e("Ciclo", "Activity: Metodo onSavedInstanceState() chamado");
}
@Override
public void onStop() {
super.onStop();
Log.e("Ciclo", "Activity: Metodo onStop() chamado");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e("Ciclo", "Activity: Metodo onDestroy() chamado");
}
private void closeKeyboard() {
View view = getActivity().getCurrentFocus();
if(view!= null){
InputMethodManager imm=(InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(),0);
}
}
private void Limpar() {
Intent i = new Intent(getApplicationContext(), IndexActivity.class);
startActivity(i);
}
}
Post your code, will help give an answer
– Costamilam
Ready, code posted from my Ragment.
– Lucas Assis
The problem found is that unfortunately the onStop method it is called, and I do not want Fragment to perform this method.
– Lucas Assis
exactly where onstop gets in your way?
– Wallace Roberto
i have a recyclerview that carries thousands of products. Therefore, it takes a while to list. When onStop is called and consequently my entire Fragment becomes null, when I go to the Fragment screen again, all data needs to be loaded. that’s the problem :/
– Lucas Assis
If you load the data into onCreateView, calling onStop will n reload the items when going back to Fragment
– Woton Sampaio
What you are doing is opening the Fragment again, and not calling the onResume, the only pause onStop, it will n destroy your data
– Woton Sampaio
Another thing, is your Fragment loaded on tabs? If it is, it is really destroyed by going through +2 Ragments, what you should do is make your Viewpager increase the limit of items before destroying the Views, for that just do:
viewPager.setOffscreenPageLimit(quantidade);
– Woton Sampaio
Wow! That’s exactly what I wanted, Woton! Thank you so much!
– Lucas Assis