NoNonsense-FilePicker/docs/example/override_back_button/index.html
Jonas Kalderstam 370097f513 Fix docs links
2016-08-22 15:18:03 +02:00

589 lines
24 KiB
HTML

<!DOCTYPE html>
<html class="no-js">
<head lang="en-us">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=1,maximum-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=10" />
<title>Override the back button - NoNonsense FilePicker</title>
<meta name="generator" content="Hugo 0.16" />
<meta name="description" content="An extensible and flexible file-picker for Android.">
<link rel="canonical" href="http://spacecowboy.github.io/NoNonsense-FilePicker/example/override_back_button/">
<meta name="author" content="SpaceCowboy">
<meta property="og:url" content="http://spacecowboy.github.io/NoNonsense-FilePicker/example/override_back_button/">
<meta property="og:title" content="NoNonsense FilePicker">
<meta name="apple-mobile-web-app-title" content="NoNonsense FilePicker">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<link rel="shortcut icon" type="image/x-icon" href="http://spacecowboy.github.io/NoNonsense-FilePicker/images/favicon.ico">
<link rel="icon" type="image/x-icon" href="http://spacecowboy.github.io/NoNonsense-FilePicker/images/favicon.ico">
<style>
@font-face {
font-family: 'Icon';
src: url('http://spacecowboy.github.io/NoNonsense-FilePicker/fonts/icon.eot?52m981');
src: url('http://spacecowboy.github.io/NoNonsense-FilePicker/fonts/icon.eot?#iefix52m981')
format('embedded-opentype'),
url('http://spacecowboy.github.io/NoNonsense-FilePicker/fonts/icon.woff?52m981')
format('woff'),
url('http://spacecowboy.github.io/NoNonsense-FilePicker/fonts/icon.ttf?52m981')
format('truetype'),
url('http://spacecowboy.github.io/NoNonsense-FilePicker/fonts/icon.svg?52m981#icon')
format('svg');
font-weight: normal;
font-style: normal;
}
</style>
<link rel="stylesheet" href="http://spacecowboy.github.io/NoNonsense-FilePicker/stylesheets/application.css">
<link rel="stylesheet" href="http://spacecowboy.github.io/NoNonsense-FilePicker/stylesheets/temporary.css">
<link rel="stylesheet" href="http://spacecowboy.github.io/NoNonsense-FilePicker/stylesheets/palettes.css">
<link rel="stylesheet" href="http://spacecowboy.github.io/NoNonsense-FilePicker/stylesheets/highlight/highlight.css">
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Roboto:400,700|Roboto%2bMono">
<style>
body, input {
font-family: 'Roboto', Helvetica, Arial, sans-serif;
}
pre, code {
font-family: 'Roboto Mono', 'Courier New', 'Courier', monospace;
}
</style>
<script src="http://spacecowboy.github.io/NoNonsense-FilePicker/javascripts/modernizr.js"></script>
</head>
<body class="palette-primary-red palette-accent-light green">
<div class="backdrop">
<div class="backdrop-paper"></div>
</div>
<input class="toggle" type="checkbox" id="toggle-drawer">
<input class="toggle" type="checkbox" id="toggle-search">
<label class="toggle-button overlay" for="toggle-drawer"></label>
<header class="header">
<nav aria-label="Header">
<div class="bar default">
<div class="button button-menu" role="button" aria-label="Menu">
<label class="toggle-button icon icon-menu" for="toggle-drawer">
<span></span>
</label>
</div>
<div class="stretch">
<div class="title">
Override the back button
</div>
</div>
<div class="button button-github" role="button" aria-label="GitHub">
<a href="https://github.com/spacecowboy" title="@spacecowboy on GitHub" target="_blank" class="toggle-button icon icon-github"></a>
</div>
</div>
<div class="bar search">
<div class="button button-close" role="button" aria-label="Close">
<label class="toggle-button icon icon-back" for="toggle-search"></label>
</div>
<div class="stretch">
<div class="field">
<input class="query" type="text" placeholder="Search" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck>
</div>
</div>
<div class="button button-reset" role="button" aria-label="Search">
<button class="toggle-button icon icon-close" id="reset-search"></button>
</div>
</div>
</nav>
</header>
<main class="main">
<div class="drawer">
<nav aria-label="Navigation">
<a href="https://github.com/spacecowboy/NoNonsense-FilePicker" class="project">
<div class="banner">
<div class="name">
<strong>NoNonsense FilePicker </strong>
<br>
spacecowboy/NoNonsense-FilePicker
</div>
</div>
</a>
<div class="scrollable">
<div class="wrapper">
<ul class="repo">
<li class="repo-download">
<a href="https://github.com/spacecowboy/NoNonsense-FilePicker/archive/master.zip" target="_blank" title="Download" data-action="download">
<i class="icon icon-download"></i> Download
</a>
</li>
<li class="repo-stars">
<a href="https://github.com/spacecowboy/NoNonsense-FilePicker/stargazers" target="_blank" title="Stargazers" data-action="star">
<i class="icon icon-star"></i> Stars
<span class="count">&ndash;</span>
</a>
</li>
</ul>
<hr>
<div class="toc">
<ul>
<li>
<a title="Readme" href="http://spacecowboy.github.io/NoNonsense-FilePicker/readme/">
Readme
</a>
</li>
<li>
<a title="Changelog" href="http://spacecowboy.github.io/NoNonsense-FilePicker/changelog/">
Changelog
</a>
</li>
<li>
<a title="License" href="http://spacecowboy.github.io/NoNonsense-FilePicker/license/">
License
</a>
</li>
<li>
<span class="section">Examples</span>
<ul>
<a title="Change the sort order" href="http://spacecowboy.github.io/NoNonsense-FilePicker/example/sortorder/">
Change the sort order
</a>
<a title="Filter based on file extension" href="http://spacecowboy.github.io/NoNonsense-FilePicker/example/filter_file_extension/">
Filter based on file extension
</a>
<a title="Custom item layout" href="http://spacecowboy.github.io/NoNonsense-FilePicker/example/custom_item_layout/">
Custom item layout
</a>
<a class="current" title="Override the back button" href="http://spacecowboy.github.io/NoNonsense-FilePicker/example/override_back_button/">
Override the back button
</a>
<ul id="scrollspy">
</ul>
<a title="Override selection behavior" href="http://spacecowboy.github.io/NoNonsense-FilePicker/example/override_selection_behavior/">
Override selection behavior
</a>
<a title="Standalone fragment" href="http://spacecowboy.github.io/NoNonsense-FilePicker/example/standalone_fragment/">
Standalone fragment
</a>
</ul>
</li>
</ul>
<hr>
<span class="section">The author</span>
<ul>
<li>
<a href="https://github.com/spacecowboy" target="_blank" title="@spacecowboy on GitHub">
@spacecowboy on GitHub
</a>
</li>
</ul>
</div>
</div>
</div>
</nav>
</div>
<article class="article">
<div class="wrapper">
<h1>Override the back button </h1>
<p>In case you want the back button to navigate the hierarchy instead of
instantly exiting the activity, this is one approach you might take.</p>
<h2 id="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</h2>
<div class="highlight" style="background: #f0f0f0"><pre style="line-height: 125%"><code><span></span><span style="color: #007020; font-weight: bold">package</span> <span style="color: #0e84b5; font-weight: bold">com.nononsenseapps.filepicker.examples.backbutton</span><span style="color: #666666">;</span>
<span style="color: #007020; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">android.os.Environment</span><span style="color: #666666">;</span>
<span style="color: #007020; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">com.nononsenseapps.filepicker.AbstractFilePickerFragment</span><span style="color: #666666">;</span>
<span style="color: #007020; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">com.nononsenseapps.filepicker.FilePickerActivity</span><span style="color: #666666">;</span>
<span style="color: #007020; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">java.io.File</span><span style="color: #666666">;</span>
<span style="color: #007020; font-weight: bold">public</span> <span style="color: #007020; font-weight: bold">class</span> <span style="color: #0e84b5; font-weight: bold">BackHandlingFilePickerActivity</span> <span style="color: #007020; font-weight: bold">extends</span> FilePickerActivity <span style="color: #666666">{</span>
<span style="color: #60a0b0; font-style: italic">/**</span>
<span style="color: #60a0b0; font-style: italic"> * Need access to the fragment</span>
<span style="color: #60a0b0; font-style: italic"> */</span>
BackHandlingFilePickerFragment currentFragment<span style="color: #666666">;</span>
<span style="color: #60a0b0; font-style: italic">/**</span>
<span style="color: #60a0b0; font-style: italic"> * Return a copy of the new fragment and set the variable above.</span>
<span style="color: #60a0b0; font-style: italic"> */</span>
<span style="color: #555555; font-weight: bold">@Override</span>
<span style="color: #007020; font-weight: bold">protected</span> AbstractFilePickerFragment<span style="color: #666666">&lt;</span>File<span style="color: #666666">&gt;</span> <span style="color: #06287e">getFragment</span><span style="color: #666666">(</span>
<span style="color: #007020; font-weight: bold">final</span> String startPath<span style="color: #666666">,</span> <span style="color: #007020; font-weight: bold">final</span> <span style="color: #902000">int</span> mode<span style="color: #666666">,</span> <span style="color: #007020; font-weight: bold">final</span> <span style="color: #902000">boolean</span> allowMultiple<span style="color: #666666">,</span>
<span style="color: #007020; font-weight: bold">final</span> <span style="color: #902000">boolean</span> allowDirCreate<span style="color: #666666">,</span> <span style="color: #007020; font-weight: bold">final</span> <span style="color: #902000">boolean</span> allowExistingFile<span style="color: #666666">,</span>
<span style="color: #007020; font-weight: bold">final</span> <span style="color: #902000">boolean</span> singleClick<span style="color: #666666">)</span> <span style="color: #666666">{</span>
<span style="color: #60a0b0; font-style: italic">// startPath is allowed to be null.</span>
<span style="color: #60a0b0; font-style: italic">// In that case, default folder should be SD-card and not &quot;/&quot;</span>
String path <span style="color: #666666">=</span> <span style="color: #666666">(</span>startPath <span style="color: #666666">!=</span> <span style="color: #007020; font-weight: bold">null</span> <span style="color: #666666">?</span> startPath
<span style="color: #666666">:</span> Environment<span style="color: #666666">.</span><span style="color: #4070a0">getExternalStorageDirectory</span><span style="color: #666666">().</span><span style="color: #4070a0">getPath</span><span style="color: #666666">());</span>
currentFragment <span style="color: #666666">=</span> <span style="color: #007020; font-weight: bold">new</span> BackHandlingFilePickerFragment<span style="color: #666666">();</span>
currentFragment<span style="color: #666666">.</span><span style="color: #4070a0">setArgs</span><span style="color: #666666">(</span>path<span style="color: #666666">,</span> mode<span style="color: #666666">,</span> allowMultiple<span style="color: #666666">,</span> allowDirCreate<span style="color: #666666">,</span>
allowExistingFile<span style="color: #666666">,</span> singleClick<span style="color: #666666">);</span>
<span style="color: #007020; font-weight: bold">return</span> currentFragment<span style="color: #666666">;</span>
<span style="color: #666666">}</span>
<span style="color: #60a0b0; font-style: italic">/**</span>
<span style="color: #60a0b0; font-style: italic"> * Override the back-button.</span>
<span style="color: #60a0b0; font-style: italic"> */</span>
<span style="color: #555555; font-weight: bold">@Override</span>
<span style="color: #007020; font-weight: bold">public</span> <span style="color: #902000">void</span> <span style="color: #06287e">onBackPressed</span><span style="color: #666666">()</span> <span style="color: #666666">{</span>
<span style="color: #60a0b0; font-style: italic">// If at top most level, normal behaviour</span>
<span style="color: #007020; font-weight: bold">if</span> <span style="color: #666666">(</span>currentFragment<span style="color: #666666">.</span><span style="color: #4070a0">isBackTop</span><span style="color: #666666">())</span> <span style="color: #666666">{</span>
<span style="color: #007020; font-weight: bold">super</span><span style="color: #666666">.</span><span style="color: #4070a0">onBackPressed</span><span style="color: #666666">();</span>
<span style="color: #666666">}</span> <span style="color: #007020; font-weight: bold">else</span> <span style="color: #666666">{</span>
<span style="color: #60a0b0; font-style: italic">// Else go up</span>
currentFragment<span style="color: #666666">.</span><span style="color: #4070a0">goUp</span><span style="color: #666666">();</span>
<span style="color: #666666">}</span>
<span style="color: #666666">}</span>
<span style="color: #666666">}</span>
</code></pre></div>
<h2 id="in-your-custom-fragment-implement-the-goup-and-isbacktop-methods">In your custom fragment, implement the goUp and isBackTop methods</h2>
<div class="highlight" style="background: #f0f0f0"><pre style="line-height: 125%"><code><span></span><span style="color: #007020; font-weight: bold">package</span> <span style="color: #0e84b5; font-weight: bold">com.nononsenseapps.filepicker.examples.backbutton</span><span style="color: #666666">;</span>
<span style="color: #007020; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">com.nononsenseapps.filepicker.FilePickerFragment</span><span style="color: #666666">;</span>
<span style="color: #007020; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">java.io.File</span><span style="color: #666666">;</span>
<span style="color: #007020; font-weight: bold">public</span> <span style="color: #007020; font-weight: bold">class</span> <span style="color: #0e84b5; font-weight: bold">BackHandlingFilePickerFragment</span> <span style="color: #007020; font-weight: bold">extends</span> FilePickerFragment <span style="color: #666666">{</span>
<span style="color: #60a0b0; font-style: italic">/**</span>
<span style="color: #60a0b0; font-style: italic"> * For consistency, the top level the back button checks against should be the start path.</span>
<span style="color: #60a0b0; font-style: italic"> * But it will fall back on /.</span>
<span style="color: #60a0b0; font-style: italic"> */</span>
<span style="color: #007020; font-weight: bold">public</span> File <span style="color: #06287e">getBackTop</span><span style="color: #666666">()</span> <span style="color: #666666">{</span>
<span style="color: #007020; font-weight: bold">return</span> getPath<span style="color: #666666">(</span>getArguments<span style="color: #666666">().</span><span style="color: #4070a0">getString</span><span style="color: #666666">(</span>KEY_START_PATH<span style="color: #666666">,</span> <span style="color: #4070a0">&quot;/&quot;</span><span style="color: #666666">));</span>
<span style="color: #666666">}</span>
<span style="color: #60a0b0; font-style: italic">/**</span>
<span style="color: #60a0b0; font-style: italic"> * @return true if the current path is the startpath or /</span>
<span style="color: #60a0b0; font-style: italic"> */</span>
<span style="color: #007020; font-weight: bold">public</span> <span style="color: #902000">boolean</span> <span style="color: #06287e">isBackTop</span><span style="color: #666666">()</span> <span style="color: #666666">{</span>
<span style="color: #007020; font-weight: bold">return</span> <span style="color: #40a070">0</span> <span style="color: #666666">==</span> compareFiles<span style="color: #666666">(</span>mCurrentPath<span style="color: #666666">,</span> getBackTop<span style="color: #666666">())</span> <span style="color: #666666">||</span>
<span style="color: #40a070">0</span> <span style="color: #666666">==</span> compareFiles<span style="color: #666666">(</span>mCurrentPath<span style="color: #666666">,</span> <span style="color: #007020; font-weight: bold">new</span> File<span style="color: #666666">(</span><span style="color: #4070a0">&quot;/&quot;</span><span style="color: #666666">));</span>
<span style="color: #666666">}</span>
<span style="color: #60a0b0; font-style: italic">/**</span>
<span style="color: #60a0b0; font-style: italic"> * Go up on level, same as pressing on &quot;..&quot;.</span>
<span style="color: #60a0b0; font-style: italic"> */</span>
<span style="color: #007020; font-weight: bold">public</span> <span style="color: #902000">void</span> <span style="color: #06287e">goUp</span><span style="color: #666666">()</span> <span style="color: #666666">{</span>
mCurrentPath <span style="color: #666666">=</span> getParent<span style="color: #666666">(</span>mCurrentPath<span style="color: #666666">);</span>
mCheckedItems<span style="color: #666666">.</span><span style="color: #4070a0">clear</span><span style="color: #666666">();</span>
mCheckedVisibleViewHolders<span style="color: #666666">.</span><span style="color: #4070a0">clear</span><span style="color: #666666">();</span>
refresh<span style="color: #666666">(</span>mCurrentPath<span style="color: #666666">);</span>
<span style="color: #666666">}</span>
<span style="color: #666666">}</span>
</code></pre></div>
<h2 id="example-manifest">Example manifest</h2>
<p>Make sure <code>android-theme</code> points to the correct theme.</p>
<div class="highlight" style="background: #f0f0f0"><pre style="line-height: 125%"><code><span></span><span style="color: #062873; font-weight: bold">&lt;manifest</span> <span style="color: #4070a0">xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</span>
<span style="color: #4070a0">package=&quot;com.nononsenseapps.filepicker.examples&quot;</span><span style="color: #062873; font-weight: bold">&gt;</span>
<span style="color: #062873; font-weight: bold">&lt;uses-permission</span> <span style="color: #4070a0">android:name=&quot;android.permission.READ_EXTERNAL_STORAGE&quot;</span> <span style="color: #062873; font-weight: bold">/&gt;</span>
<span style="color: #60a0b0; font-style: italic">&lt;!-- Only needed to create sub directories. --&gt;</span>
<span style="color: #062873; font-weight: bold">&lt;uses-permission</span> <span style="color: #4070a0">android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot;</span> <span style="color: #062873; font-weight: bold">/&gt;</span>
<span style="color: #062873; font-weight: bold">&lt;application</span>
<span style="color: #4070a0">android:allowBackup=&quot;true&quot;</span>
<span style="color: #4070a0">android:icon=&quot;@mipmap/ic_launcher&quot;</span>
<span style="color: #4070a0">android:label=&quot;@string/app_name&quot;</span>
<span style="color: #4070a0">android:supportsRtl=&quot;true&quot;</span>
<span style="color: #4070a0">android:theme=&quot;@style/FilePickerTheme&quot;</span><span style="color: #062873; font-weight: bold">&gt;</span>
<span style="color: #062873; font-weight: bold">&lt;activity</span>
<span style="color: #4070a0">android:name=&quot;.backbutton.BackHandlingFilePickerActivity&quot;</span>
<span style="color: #4070a0">android:label=&quot;Override back button&quot;</span>
<span style="color: #4070a0">android:theme=&quot;@style/FilePickerTheme&quot;</span><span style="color: #062873; font-weight: bold">&gt;</span>
<span style="color: #062873; font-weight: bold">&lt;/activity&gt;</span>
<span style="color: #062873; font-weight: bold">&lt;/application&gt;</span>
<span style="color: #062873; font-weight: bold">&lt;/manifest&gt;</span>
</code></pre></div>
<aside class="copyright" role="note">
&copy; 2016 Released under the Mozilla Public License 2.0 &ndash;
Documentation built with
<a href="https://www.gohugo.io" target="_blank">Hugo</a>
using the
<a href="http://github.com/digitalcraftsman/hugo-material-docs" target="_blank">Material</a> theme.
</aside>
<footer class="footer">
<nav class="pagination" aria-label="Footer">
<div class="previous">
<a href="http://spacecowboy.github.io/NoNonsense-FilePicker/example/override_selection_behavior/" title="Override selection behavior">
<span class="direction">
Previous
</span>
<div class="page">
<div class="button button-previous" role="button" aria-label="Previous">
<i class="icon icon-back"></i>
</div>
<div class="stretch">
<div class="title">
Override selection behavior
</div>
</div>
</div>
</a>
</div>
<div class="next">
<a href="http://spacecowboy.github.io/NoNonsense-FilePicker/example/filter_file_extension/" title="Filter based on file extension">
<span class="direction">
Next
</span>
<div class="page">
<div class="stretch">
<div class="title">
Filter based on file extension
</div>
</div>
<div class="button button-next" role="button" aria-label="Next">
<i class="icon icon-forward"></i>
</div>
</div>
</a>
</div>
</nav>
</footer>
</div>
</article>
<div class="results" role="status" aria-live="polite">
<div class="scrollable">
<div class="wrapper">
<div class="meta"></div>
<div class="list"></div>
</div>
</div>
</div>
</main>
<script>
var base_url = 'http:\/\/spacecowboy.github.io\/NoNonsense-FilePicker\/';
var repo_id = 'spacecowboy\/NoNonsense-FilePicker';
</script>
<script src="http://spacecowboy.github.io/NoNonsense-FilePicker/javascripts/application.js"></script>
<script>
/* Add headers to scrollspy */
var headers = document.getElementsByTagName("h2");
var scrollspy = document.getElementById('scrollspy');
if(scrollspy) {
if(headers.length > 0) {
for(var i = 0; i < headers.length; i++) {
var li = document.createElement("li");
li.setAttribute("class", "anchor");
var a = document.createElement("a");
a.setAttribute("href", "#" + headers[i].id);
a.setAttribute("title", headers[i].innerHTML);
a.innerHTML = headers[i].innerHTML;
li.appendChild(a)
scrollspy.appendChild(li);
}
} else {
scrollspy.parentElement.removeChild(scrollspy)
}
/* Add permanent link next to the headers */
var headers = document.querySelectorAll("h1, h2, h3, h4, h5, h6");
for(var i = 0; i < headers.length; i++) {
var a = document.createElement("a");
a.setAttribute("class", "headerlink");
a.setAttribute("href", "#" + headers[i].id);
a.setAttribute("title", "Permanent link")
a.innerHTML = "#";
headers[i].appendChild(a);
}
}
</script>
<script src="//gohugo.io/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
</body>
</html>