Home avoid app exit on pressing back button
Reply: 0

avoid app exit on pressing back button

user10600
1#
user10600 Published in June 19, 2018, 11:55 pm

I have one main activity and one adapter in a file manager app.So when I open a folder in the app, then open a subfolder, and then press back button, its back to the home screen/main screen.
I want to go back to the subfolders screen on pressing Back, then to the folders screen on pressing back, then to the main screen on pressing back..

But right now, when I press back button I directly come back to the main screen.

How can I achieve this ?

InternalStorage.java:

public class InternalStorage extends AppCompatActivity implements MyRecyclerViewAdapter.ItemClickListener {
    MyRecyclerViewAdapter adapter;
    private ArrayList<String> myList, myList2,;
    String path = Environment.getExternalStorageDirectory().getAbsolutePath();
    public static boolean selectallflag = false;
    RecyclerView recyclerView;
    private static final String TAG = "com.example.dell_1.myapp3.InternalMemory";
    File f = new File(path);//converted string object to file//getting the list of files in string array

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_internal_storage);

        method1(f);
        method2(f);

        // set up the RecyclerView
       setAdapter();

    }

    @Override
    public void onItemClick(View view, int position) {
        String string1 = adapter.getItem(position);
        final File directory = new File(string1);
        if (directory.isDirectory()) {
            method1(directory);
            method2(directory);
           setAdapter():
        } else if (string1.endsWith(".mp3")) {
            Intent viewIntent1 = new Intent(Intent.ACTION_VIEW);
            viewIntent1.setDataAndType(Uri.fromFile(directory), "audio/mpeg");
            startActivity(Intent.createChooser(viewIntent1, null));
        } else if (string1.endsWith(".zip")) {
            Intent viewIntent1 = new Intent(Intent.ACTION_VIEW);
            viewIntent1.setDataAndType(Uri.fromFile(directory), "application/zip");
            startActivity(Intent.createChooser(viewIntent1, null));
        } else if (string1.endsWith(".mp4")) {
            Intent viewIntent1 = new Intent(Intent.ACTION_VIEW);
            viewIntent1.setDataAndType(Uri.fromFile(directory), "video/mp4");
            startActivity(Intent.createChooser(viewIntent1, null));
        } else if (string1.endsWith(".jpeg")) {
            Intent viewIntent1 = new Intent(Intent.ACTION_VIEW);
            viewIntent1.setDataAndType(Uri.fromFile(directory), "image/*");
            startActivity(Intent.createChooser(viewIntent1, null));
        } else if (string1.endsWith(".png")) {
            Intent viewIntent1 = new Intent(Intent.ACTION_VIEW);
            viewIntent1.setDataAndType(Uri.fromFile(directory), "image/*");
            startActivity(Intent.createChooser(viewIntent1, null));
        } else if (string1.endsWith(".pdf")) {
            Intent viewIntent1 = new Intent(Intent.ACTION_VIEW);
            viewIntent1.setDataAndType(Uri.fromFile(directory), "application/pdf");
            startActivity(Intent.createChooser(viewIntent1, null));
        } else if (string1.endsWith(".apk")) {
            Intent viewIntent1 = new Intent(Intent.ACTION_VIEW);
            viewIntent1.setDataAndType(Uri.fromFile(directory), "application/vnd.android.package-archive");
            startActivity(Intent.createChooser(viewIntent1, null));
        } else if (string1.endsWith(".txt")) {
            Intent viewIntent1 = new Intent(Intent.ACTION_VIEW);
            viewIntent1.setDataAndType(Uri.fromFile(directory), "text/*");
            startActivity(Intent.createChooser(viewIntent1, null));
        } else Toast.makeText(this, "unsupported format", Toast.LENGTH_SHORT)
                .show();
    }


    public ArrayList<String> method1(File f) {
        File list2[] = f.listFiles();
        myList = new ArrayList<>();
        if (list2 != null) {
            for (int i = 0; i < list2.length; i++) {
                myList.add(list2[i].getName());
            }
        } else Toast.makeText(this, "the folder is empty", Toast.LENGTH_SHORT)
                .show();
        return myList;
    }

    public ArrayList<String> method2(File f) {
        File list2[] = f.listFiles();
        myList2 = new ArrayList<>();
        if (list2 != null) {
            for (int i = 0; i < list2.length; i++) {
                myList2.add(list2[i].getPath());
            }
        } else Toast.makeText(this, "the folder is empty", Toast.LENGTH_SHORT)
                .show();
        return myList2;
    }


    @Override
    public void onBackPressed() {
        selectallflag =false;
        super.onBackPressed();
    }

 private void setAdapter(){
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rvNumbers);
        int numberOfColumns = 4;
        recyclerView.setLayoutManager(new GridLayoutManager(this, numberOfColumns));
        adapter = new MyRecyclerViewAdapter(this, myList, myList2);
        adapter.setClickListener(this);
        recyclerView.setAdapter(adapter);
    }

}

MyRecyclerViewAdapter.java:

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {

    private ArrayList<String> mData;
    private ArrayList<String> mData2;
    private LayoutInflater mInflater;
    private int selected_position ;
    private ItemClickListener mClickListener;
    private ArrayList<String> mSelected = new ArrayList<>();
    ArrayList<Uri> files = new ArrayList<>();
    private static final String TAG = "com.example.dell_1.myapp3.InternalMemory";
    private Context context;

    // data is passed into the constructor
    public MyRecyclerViewAdapter(Context context, ArrayList<String> data, ArrayList<String> data2) {
        this.mInflater = LayoutInflater.from(context);
        this.mData = data;
        this.mData2 = data2;
        this.context = context;
    }

    // inflates the cell layout from xml when needed
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.recyclerview_item, parent, false);
        return new ViewHolder(view);
    }

    // binds the data to the textview in each cell
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        String animal = mData.get(position);
        String animal2 = mData2.get(position);
        int THUMBSIZE = 150;
        Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(animal2),
                THUMBSIZE, THUMBSIZE);
        Bitmap thumb = ThumbnailUtils.createVideoThumbnail(animal2, MediaStore.Video.Thumbnails.MINI_KIND);
        holder.myTextView.setText(animal + "");
            if(animal!= null && animal.endsWith(".mp3")){
                holder.myImage.setImageResource(R.drawable.song);
            }
        else if(animal!= null && animal.endsWith(".pdf")){
            holder.myImage.setImageResource(R.drawable.pdficon2);
        }
        else
            if(animal!= null && animal.endsWith(".jpeg") && BitmapFactory.decodeFile(animal2)!=null ){
                holder.myImage.setImageBitmap(ThumbImage);
            }
            else
            if(animal!= null && animal.endsWith(".png") && BitmapFactory.decodeFile(animal2)!=null ){
                holder.myImage.setImageBitmap(ThumbImage);
            }
            else
            if(animal!= null && animal.endsWith(".mp4")){
                holder.myImage.setImageBitmap(thumb);
            }
            else
            if(animal!= null && animal.endsWith(".zip")){
                holder.myImage.setImageResource(R.drawable.zip);
            }
            else
            if(animal!= null && animal.endsWith(".aac")){
                holder.myImage.setImageResource(R.drawable.song);
            }
            else
            if(animal!= null && animal.endsWith(".txt")){
                holder.myImage.setImageResource(R.drawable.text);
            }
           else if(animal!= null && animal.endsWith(".apk")){
            PackageInfo packageInfo = context.getPackageManager()
                    .getPackageArchiveInfo(animal2, PackageManager.GET_ACTIVITIES);
            if(packageInfo != null) {
                ApplicationInfo appInfo = packageInfo.applicationInfo;
                    appInfo.sourceDir = animal2;
                    appInfo.publicSourceDir = animal2;
                Drawable icon = appInfo.loadIcon(context.getPackageManager());
                Bitmap bmpIcon = ((BitmapDrawable) icon).getBitmap();
                holder.myImage.setImageBitmap(bmpIcon);
            }

        }
            else {
                holder.myImage.setImageResource(R.drawable.folder);
            }

        if(selectallflag){
            holder.itemView.setBackgroundColor(Color.MAGENTA);
            }
        }


    // total number of cells
    @Override
    public int getItemCount() {
        return mData2.size();
    }


    // stores and recycles views as they are scrolled off screen
    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
        TextView myTextView;
        ImageButton myImage;
        ViewHolder(View itemView) {
            super(itemView);
            myImage = (ImageButton) itemView.findViewById(R.id.buttonimage);
            myTextView = (TextView) itemView.findViewById(R.id.info_text);
            myImage.setOnClickListener(this);
            myImage.setOnLongClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
        }

    // convenience method for getting data at click position
    public String getItem(int id) {
        return mData2.get(id);
    }

    // allows clicks events to be caught
    public void setClickListener(ItemClickListener itemClickListener) {
        this.mClickListener = itemClickListener;
    }

    // parent activity will implement this method to respond to click events
    public interface ItemClickListener {
        void onItemClick(View view, int position);
        boolean onLongClick(View view,int position);
    }
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.330274 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO