-1

I am currently developing an Android mobile application and I encountered a problem. Although I tried and searched many sites and resources in stackoverflow, I could not find the solution. I am sharing my codes below.

What I want to do i want the fragments to go back when the back button is pressed, but it goes back to the activity part. As a result of how many Fragments are clicked, it returns to the activity part. What can I do. Sorry for my missing English.

InternalFilesActivity

 public class InternalFilesActivity extends AppCompatActivity {
        private FragmentManager manager;
        private FragmentTransaction transaction;
        private File[] files;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_internal_files);
    
            String path = Environment.getExternalStorageDirectory().toString();
            File directory = new File(path);
            files = directory.listFiles();
    
            manager = getSupportFragmentManager();
            Fragment fr = FilesFragment.newInstance(files);
            transaction = manager.beginTransaction();
            transaction.add(R.id.fragment_container, fr);
            transaction.commit();
    
        }
    
        @Override
        public void onBackPressed() {
            if (manager.getBackStackEntryCount() > 1) {
                manager.popBackStackImmediate();
            } else {
                super.onBackPressed();
            }
        }
    }

FilesFragment

public class FilesFragment extends Fragment {
    private static File[] frFiles;

    public FilesFragment() {
    }

    public static FilesFragment newInstance(File[] files) {
        FilesFragment fragment = new FilesFragment();
        frFiles = files;
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_files, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ListView internalList = view.findViewById(R.id.lv_internal);
        InternalListAdapter adapter = new InternalListAdapter(frFiles, getContext(), getActivity());
        internalList.setAdapter(adapter);
    }
}

InternalListAdapter

public class InternalListAdapter extends BaseAdapter {
    public File[] dataList;
    public Context context;
    public FragmentActivity activity;


    public InternalListAdapter(File[] dataList, Context context, FragmentActivity activity) {
        this.dataList = dataList;
        this.context = context;
        this.activity = activity;
    }

    @Override
    public int getCount() {
        return dataList.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null)
            convertView = LayoutInflater.from(context).inflate(R.layout.internal_list_item, parent, false);

        TextView fileName = convertView.findViewById(R.id.tv_file_name);
        fileName.setText(dataList[position].getName());

        ImageView listImage = convertView.findViewById(R.id.iv_list_image);
        if (dataList[position].isDirectory()) {
            listImage.setImageResource(R.drawable.ic_directory);
        } else {
            listImage.setImageResource(R.drawable.ic_file);
        }

        LinearLayout listItemLayout = convertView.findViewById(R.id.ll_list_item);
        listItemLayout.setOnClickListener(v -> {

            if (dataList[position].isDirectory()) {
                createFragment(position);
            }
        });

        return convertView;
    }

    private void createFragment(int position) {
        FragmentManager manager = activity.getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        Fragment fr = FilesFragment.newInstance(dataList[position].listFiles());
        transaction.replace(R.id.fragment_container, fr);
        transaction.commit();
    }

}
fhidiroglu
  • 145
  • 2
  • 14

3 Answers3

0

in back press you have implemented Fragment popping up from back stack, but you are not adding your fragments to back stack in fact

use addToBackStack(someName) method for putting your fragments in there and then your back press should start working (more info in this SO topic)

        manager = getSupportFragmentManager();
        Fragment fr = FilesFragment.newInstance(files);
        transaction = manager.beginTransaction();
        transaction.addToBackStack("initialFragment"); // also may be null
        transaction.add(R.id.fragment_container, fr);
        transaction.commit();
snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • I've tried these before but it didn't work. I tried the addToBackStack () method with 'null' and 'tag', but it didn't work either way. @snachmsm – fhidiroglu Apr 06 '21 at 11:17
0

check my answer.

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
          int count = getSupportFragmentManager().getBackStackEntryCount();
            if(count==1){
                finish();
            }
        }
        return super.onKeyDown(keyCode,event);
    }
Anwar Zahid
  • 227
  • 3
  • 10
0

SOLVED

The problem was that I used fragment as an instance. The problem was resolved when I created and used a new object.

private static File[] files;

public FilesFragment(File[] files) {
  this.files = files;     
}

I used the new FileManager() code instead of FileFragment.newInstance()

fhidiroglu
  • 145
  • 2
  • 14