Update back buttone example and add as actual code

This commit is contained in:
Jonas Kalderstam 2016-08-22 14:46:00 +02:00
parent e6f4f17862
commit 3171e8a01c
18 changed files with 250 additions and 10 deletions

View File

@ -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 {
/**
@ -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;
}
@ -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 {
/**
@ -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
examples/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/build

27
examples/build.gradle Normal file
View File

@ -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
examples/proguard-rules.pro vendored Normal file
View File

@ -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 *;
#}

View File

@ -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);
}
}

View File

@ -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>

View File

@ -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();
}
}
}

View File

@ -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 not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

View File

@ -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>

View File

@ -0,0 +1,3 @@
<resources>
<string name="app_name">Examples</string>
</resources>

View File

@ -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>

View File

@ -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);
}
}

View File

@ -4,4 +4,4 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
include ':sample', ':library'
include ':sample', ':library', ':examples'