Skip to content

Commit 3ce1793

Browse files
committed
Receiving back the proper filepath when back button is pressed
1 parent 02e537b commit 3ce1793

File tree

3 files changed

+12
-13
lines changed

3 files changed

+12
-13
lines changed

simplefileexplorer/src/main/java/com/example/simplefileexplorer/ActivityListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
interface ActivityListener extends Serializable {
66
void onDirectoryChanged(String absolutePath);
77
void onFileSelect(String absolutePath);
8-
void updateFileTypeIcon();
8+
void onBackButtonPressed(String absolutePath);
99
}

simplefileexplorer/src/main/java/com/example/simplefileexplorer/SimpleFileExplorerActivity.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.app.Activity;
55
import android.content.Intent;
66
import android.content.pm.PackageManager;
7+
import android.os.Environment;
78
import android.support.v4.app.FragmentManager;
89
import android.support.v4.content.ContextCompat;
910
import android.support.v7.app.AppCompatActivity;
@@ -34,14 +35,8 @@ protected void onCreate(Bundle savedInstanceState) {
3435
this.setSelectButtonClickListener();
3536
SimpleFileExplorerFragment fragment = new SimpleFileExplorerFragment();
3637
fragment.setListeners(this);
37-
getSupportFragmentManager().beginTransaction().add(R.id.frame_layout, fragment).addToBackStack(this.STACK_KEY).commit();
38-
39-
getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
40-
@Override
41-
public void onBackStackChanged() {
42-
selectButton.setEnabled(false);
43-
}
44-
});
38+
getSupportFragmentManager().beginTransaction().add(R.id.frame_layout, fragment).addToBackStack(STACK_KEY).commit();
39+
this.selectedAbsolutePath = Environment.getExternalStorageDirectory().getAbsolutePath();
4540
}
4641

4742
private void initViews(){
@@ -72,10 +67,11 @@ public void onClick(View v) {
7267

7368
@Override
7469
public void onDirectoryChanged(String absolutePath) {
70+
this.selectedAbsolutePath = absolutePath;
7571
SimpleFileExplorerFragment fragment = new SimpleFileExplorerFragment();
7672
fragment.setListeners(this);
7773
fragment.setDirectory(absolutePath);
78-
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, fragment).addToBackStack(this.STACK_KEY).commit();
74+
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, fragment).addToBackStack(STACK_KEY).commit();
7975
}
8076

8177
@Override
@@ -85,8 +81,9 @@ public void onFileSelect(String absolutePath) {
8581
}
8682

8783
@Override
88-
public void updateFileTypeIcon() {
84+
public void onBackButtonPressed(String absolutePath) {
8985
this.fileTypeImageView.setImageResource(R.drawable.ic_folder);
86+
this.selectedAbsolutePath = absolutePath;
9087
}
9188

9289
private void checkPermission(){

simplefileexplorer/src/main/java/com/example/simplefileexplorer/SimpleFileExplorerFragment.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import android.view.LayoutInflater;
1111
import android.view.View;
1212
import android.view.ViewGroup;
13-
import android.widget.Toast;
1413

1514
import java.io.File;
1615
import java.util.ArrayList;
@@ -58,6 +57,9 @@ private void loadDirectory(){
5857
if(this.selectedAbsolutePath != null){
5958
root = new File(this.selectedAbsolutePath);
6059
}
60+
else{
61+
this.selectedAbsolutePath = root.getAbsolutePath();
62+
}
6163

6264
List<FileModel> fileModelList = new ArrayList<>();
6365

@@ -108,6 +110,6 @@ void setDirectory(String dir){
108110
@Override
109111
public void onStart() {
110112
super.onStart();
111-
this.activityListener.updateFileTypeIcon();
113+
this.activityListener.onBackButtonPressed(this.selectedAbsolutePath);
112114
}
113115
}

0 commit comments

Comments
 (0)