Click here to Skip to main content
15,881,840 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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) {
        // Inflate the layout for this fragment
        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:)
Posted
Comments
David Crow 22-Jul-21 12:44pm    
What happens if you simply call finish() from within the onClick() method?
RookieStudent 22-Jul-21 21:53pm    
nothing happens. It simply crashes as the error message states that I have not declared the Categories fragment within the manifest. But however as far as I know. You can not declare fragments within the manifest can you?
David Crow 22-Jul-21 22:53pm    
"nothing happens. It simply crashes as the error message states that I have not declared the Categories fragment..."

Because you forgot to comment out those two lines.

"You can not declare fragments within the manifest can you?"

Correct. You do not "start" a fragment. It belongs to a running activity.
RookieStudent 22-Jul-21 23:20pm    
Oh my gosh haha the issue has been resolved :D Thanks man! Cant believe that all that was needed was just simply to call finish () XD. Greatly appreciate you help here !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900