Currently, as the title suggests, I am able to get to an activity. But how do I get from an activity back to a fragment is the issue that I face here.
so the following below is the fragment that I am at:
package com.example.nestedrecyclerviewtesting;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.gridlayout.widget.GridLayout;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.Toast;
import com.Categories.SauceAndPaste;
import com.example.nestedrecyclerviewtesting.databinding.ActivityMainBinding;
import java.util.ArrayList;
import java.util.List;
public class CategoriesFragment extends Fragment {
CardView cvSauceAndPaste,cvVegetables,cvChicken,cvFishAndSeafood,cvYongTaoFoo,cvFruit,cvNoodles,cvPork;
Spinner spnButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_categories, container, false);
spnButton = view.findViewById(R.id.spinner);
List<String>categories = new ArrayList<>();
categories.add(0,"Choose Category");
categories.add("Sauce & Paste");
categories.add("Vegetable");
categories.add("Chicken");
categories.add("Fish & SeaFood");
categories.add("Fruit");
categories.add("Noodles");
categories.add("Pork");
ArrayAdapter<String>dataAdapter;
dataAdapter = new ArrayAdapter(this.getActivity(), android.R.layout.simple_spinner_item,categories);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnButton.setAdapter(dataAdapter);
spnButton.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getItemAtPosition(position).equals("Choose Category")){
}else{
String item = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), "Selected: ", Toast.LENGTH_SHORT).show();
if (parent.getItemAtPosition(position).equals("Sauce & Paste"))
{
Intent i = new Intent(CategoriesFragment.this.getActivity(), SauceAndPaste.class);
startActivity(i);
}
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
return view;
}
}
hence when I have selected the SauceAndPaste, I am able to get to the activity itself successfully with the code as seen below:
public class SauceAndPaste extends AppCompatActivity {
Button btnBack;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sauce_and_paste);
btnBack = findViewById(R.id.btnBack);
btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(SauceAndPaste.this, CategoriesFragment.class);
startActivity(i);
}
});
}
}
as i then wish to get back to the CategoriesFragment screen, I have created a button to get me back. Although I am aware that using intent in this case is impossible. I simply cant figure out any other method to adopt here. Could anyone help me please?
What I have tried:
I have tried googling for solutions , but their answers are not able to assist me with the current issue i face or that it is too old. Although I am aware of using the backStack method, I am unsure on how to apply it for my case. Any help would be greatly appreciated:)