Skip to content
This repository has been archived by the owner on Feb 11, 2022. It is now read-only.

Commit

Permalink
Update back buttone example and add as actual code
Browse files Browse the repository at this point in the history
  • Loading branch information
spacecowboy committed Aug 22, 2016
1 parent e6f4f17 commit 3171e8a
Show file tree
Hide file tree
Showing 18 changed files with 250 additions and 10 deletions.
48 changes: 39 additions & 9 deletions docs-src/content/example/override_back_button.md
Expand Up @@ -11,12 +11,17 @@ title = "Override the back button"

+++

In case you want the back button navigate the hierarchy instead of instantly exiting the activity, this
is one approach you might take.
In case you want the back button to navigate the hierarchy instead of
instantly exiting the activity, this is one approach you might take.

## Create an activity which overrides the back button, and loads a custom fragment
## Create an activity which overrides the back button and loads a custom fragment

```java
import android.os.Environment;
import com.nononsenseapps.filepicker.AbstractFilePickerFragment;
import com.nononsenseapps.filepicker.FilePickerActivity;
import java.io.File;

public class BackHandlingFilePickerActivity extends FilePickerActivity {

/**
Expand All @@ -30,11 +35,17 @@ public class BackHandlingFilePickerActivity extends FilePickerActivity {
@Override
protected AbstractFilePickerFragment<File> getFragment(
final String startPath, final int mode, final boolean allowMultiple,
final boolean allowCreateDir) {
final boolean allowDirCreate, final boolean allowExistingFile,
final boolean singleClick) {

// startPath is allowed to be null.
// In that case, default folder should be SD-card and not "/"
String path = (startPath != null ? startPath
: Environment.getExternalStorageDirectory().getPath());

currentFragment = new BackHandlingFilePickerFragment();
// startPath is allowed to be null. In that case, default folder should be SD-card and not "/"
currentFragment.setArgs(startPath != null ? startPath : Environment.getExternalStorageDirectory().getPath(),
mode, allowMultiple, allowCreateDir);
currentFragment.setArgs(path, mode, allowMultiple, allowDirCreate,
allowExistingFile, singleClick);
return currentFragment;
}

Expand All @@ -54,9 +65,12 @@ public class BackHandlingFilePickerActivity extends FilePickerActivity {
}
```

## In you custom fragment, implement the goUp and isBackTop methods
## In your custom fragment, implement the goUp and isBackTop methods

```java
import com.nononsenseapps.filepicker.FilePickerFragment;
import java.io.File;

public class BackHandlingFilePickerFragment extends FilePickerFragment {

/**
Expand Down Expand Up @@ -86,7 +100,23 @@ public class BackHandlingFilePickerFragment extends FilePickerFragment {
mCurrentPath = getParent(mCurrentPath);
mCheckedItems.clear();
mCheckedVisibleViewHolders.clear();
refresh();
refresh(mCurrentPath);
}
}
```

## And finally, add the following to your manifest

And make sure `android-theme` points to the correct theme.

```xml
<activity
android:name=".BackHandlingFilePickerActivity"
android:label="@string/select_file"
android:theme="@style/FilePickerTheme">
<intent-filter>
<action android:name="android.intent.action.GET_CONTENT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
```
1 change: 1 addition & 0 deletions examples/.gitignore
@@ -0,0 +1 @@
/build
27 changes: 27 additions & 0 deletions examples/build.gradle
@@ -0,0 +1,27 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion "24.0.1"

defaultConfig {
applicationId "com.nononsenseapps.filepicker.examples"
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':library')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.0'
}
17 changes: 17 additions & 0 deletions examples/proguard-rules.pro
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /home/jonas/Android/Sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
@@ -0,0 +1,13 @@
package com.nononsenseapps.filepicker.examples;

import android.app.Application;
import android.test.ApplicationTestCase;

/**
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
*/
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
}
}
26 changes: 26 additions & 0 deletions examples/src/main/AndroidManifest.xml
@@ -0,0 +1,26 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nononsenseapps.filepicker.examples">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!-- Only needed to create sub directories. -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/FilePickerTheme">

<activity
android:name=".backbutton.BackHandlingFilePickerActivity"
android:label="Override back button"
android:theme="@style/FilePickerTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
@@ -0,0 +1,48 @@
package com.nononsenseapps.filepicker.examples.backbutton;

import android.os.Environment;
import com.nononsenseapps.filepicker.AbstractFilePickerFragment;
import com.nononsenseapps.filepicker.FilePickerActivity;
import java.io.File;

public class BackHandlingFilePickerActivity extends FilePickerActivity {

/**
* Need access to the fragment
*/
BackHandlingFilePickerFragment currentFragment;

/**
* Return a copy of the new fragment and set the variable above.
*/
@Override
protected AbstractFilePickerFragment<File> getFragment(
final String startPath, final int mode, final boolean allowMultiple,
final boolean allowDirCreate, final boolean allowExistingFile,
final boolean singleClick) {

// startPath is allowed to be null.
// In that case, default folder should be SD-card and not "/"
String path = (startPath != null ? startPath
: Environment.getExternalStorageDirectory().getPath());

currentFragment = new BackHandlingFilePickerFragment();
currentFragment.setArgs(path, mode, allowMultiple, allowDirCreate,
allowExistingFile, singleClick);
return currentFragment;
}

/**
* Override the back-button.
*/
@Override
public void onBackPressed() {
// If at top most level, normal behaviour
if (currentFragment.isBackTop()) {
super.onBackPressed();
} else {
// Else go up
currentFragment.goUp();
}
}
}
@@ -0,0 +1,37 @@
package com.nononsenseapps.filepicker.examples.backbutton;

import com.nononsenseapps.filepicker.FilePickerFragment;
import java.io.File;

public class BackHandlingFilePickerFragment extends FilePickerFragment {

/**
* For consistency, the top level the back button checks against should be the start path.
* But it will fall back on /.
*/
public File getBackTop() {
if (getArguments().containsKey(KEY_START_PATH)) {
return getPath(getArguments().getString(KEY_START_PATH));
} else {
return new File("/");
}
}

/**
*
* @return true if the current path is the startpath or /
*/
public boolean isBackTop() {
return 0 == compareFiles(mCurrentPath, getBackTop()) || 0 == compareFiles(mCurrentPath, new File("/"));
}

/**
* Go up on level, same as pressing on "..".
*/
public void goUp() {
mCurrentPath = getParent(mCurrentPath);
mCheckedItems.clear();
mCheckedVisibleViewHolders.clear();
refresh(mCurrentPath);
}
}
Binary file added examples/src/main/res/mipmap-hdpi/ic_launcher.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/src/main/res/mipmap-mdpi/ic_launcher.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions examples/src/main/res/values/colors.xml
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="primary2">#F44336</color>
<color name="primary_dark2">#D32F2F</color>
<color name="accent2">#FFAB00</color>
</resources>
3 changes: 3 additions & 0 deletions examples/src/main/res/values/strings.xml
@@ -0,0 +1,3 @@
<resources>
<string name="app_name">Examples</string>
</resources>
17 changes: 17 additions & 0 deletions examples/src/main/res/values/styles.xml
@@ -0,0 +1,17 @@
<resources>

<style name="FilePickerTheme" parent="NNF_BaseTheme">
<item name="colorPrimary">@color/primary2</item>
<item name="colorPrimaryDark">@color/primary_dark2</item>
<item name="colorAccent">@color/accent2</item>

<item name="alertDialogTheme">@style/FilePickerAlertDialogTheme</item>
</style>

<style name="FilePickerAlertDialogTheme" parent="Theme.AppCompat.Dialog.Alert">
<item name="colorPrimary">@color/primary2</item>
<item name="colorPrimaryDark">@color/primary_dark2</item>
<item name="colorAccent">@color/accent2</item>
</style>

</resources>
@@ -0,0 +1,15 @@
package com.nononsenseapps.filepicker.examples;

import org.junit.Test;

import static org.junit.Assert.*;

/**
* To work on unit tests, switch the Test Artifact in the Build Variants view.
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() throws Exception {
assertEquals(4, 2 + 2);
}
}
2 changes: 1 addition & 1 deletion settings.gradle
Expand Up @@ -4,4 +4,4 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

include ':sample', ':library'
include ':sample', ':library', ':examples'

0 comments on commit 3171e8a

Please sign in to comment.