Create fragments appropriately in main view.
The approach to creating fragments in the constructor of the `AppListFragmentPagerAdapter` was incorrect. Fragments are a special kind of magic, so this commit uses the approach documented at http://stackoverflow.com/a/15261142 to make sure that the correct fragment instances are always retrieved. Fixes #521.
This commit is contained in:
parent
4ae5045568
commit
db1f54c352
@ -4,6 +4,7 @@ import android.support.annotation.NonNull;
|
|||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
import android.support.v4.app.Fragment;
|
import android.support.v4.app.Fragment;
|
||||||
import android.support.v4.app.FragmentPagerAdapter;
|
import android.support.v4.app.FragmentPagerAdapter;
|
||||||
|
import android.view.ViewGroup;
|
||||||
|
|
||||||
import org.fdroid.fdroid.FDroid;
|
import org.fdroid.fdroid.FDroid;
|
||||||
import org.fdroid.fdroid.R;
|
import org.fdroid.fdroid.R;
|
||||||
@ -17,22 +18,34 @@ import org.fdroid.fdroid.views.fragments.InstalledAppsFragment;
|
|||||||
/**
|
/**
|
||||||
* Used by the FDroid activity in conjunction with its ViewPager to support
|
* Used by the FDroid activity in conjunction with its ViewPager to support
|
||||||
* swiping of tabs for both old devices (< 3.0) and new devices.
|
* swiping of tabs for both old devices (< 3.0) and new devices.
|
||||||
|
*
|
||||||
|
* See http://stackoverflow.com/a/15261142 for how to obtain references
|
||||||
|
* to fragments in order to update them in response to search queries.
|
||||||
*/
|
*/
|
||||||
public class AppListFragmentPagerAdapter extends FragmentPagerAdapter {
|
public class AppListFragmentPagerAdapter extends FragmentPagerAdapter {
|
||||||
|
|
||||||
@NonNull private final FDroid parent;
|
@NonNull private final FDroid parent;
|
||||||
|
@Nullable private String searchQuery;
|
||||||
|
|
||||||
@NonNull private final AppListFragment availableFragment;
|
private final AppListFragment[] registeredFragments = new AppListFragment[TabManager.INDEX_COUNT];
|
||||||
@NonNull private final AppListFragment installedFragment;
|
|
||||||
@NonNull private final AppListFragment canUpdateFragment;
|
|
||||||
|
|
||||||
public AppListFragmentPagerAdapter(@NonNull FDroid parent) {
|
public AppListFragmentPagerAdapter(@NonNull FDroid parent) {
|
||||||
super(parent.getSupportFragmentManager());
|
super(parent.getSupportFragmentManager());
|
||||||
this.parent = parent;
|
this.parent = parent;
|
||||||
|
}
|
||||||
|
|
||||||
availableFragment = new AvailableAppsFragment();
|
@Override
|
||||||
installedFragment = new InstalledAppsFragment();
|
public Object instantiateItem(ViewGroup container, int position) {
|
||||||
canUpdateFragment = new CanUpdateAppsFragment();
|
AppListFragment fragment = (AppListFragment) super.instantiateItem(container, position);
|
||||||
|
fragment.updateSearchQuery(searchQuery);
|
||||||
|
registeredFragments[position] = fragment;
|
||||||
|
return fragment;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void destroyItem(ViewGroup container, int position, Object object) {
|
||||||
|
registeredFragments[position] = null;
|
||||||
|
super.destroyItem(container, position, object);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getInstalledTabTitle() {
|
private String getInstalledTabTitle() {
|
||||||
@ -46,20 +59,23 @@ public class AppListFragmentPagerAdapter extends FragmentPagerAdapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void updateSearchQuery(@Nullable String query) {
|
public void updateSearchQuery(@Nullable String query) {
|
||||||
availableFragment.updateSearchQuery(query);
|
searchQuery = query;
|
||||||
installedFragment.updateSearchQuery(query);
|
for (AppListFragment fragment : registeredFragments) {
|
||||||
canUpdateFragment.updateSearchQuery(query);
|
if (fragment != null) {
|
||||||
|
fragment.updateSearchQuery(query);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Fragment getItem(int i) {
|
public Fragment getItem(int i) {
|
||||||
switch (i) {
|
switch (i) {
|
||||||
case TabManager.INDEX_AVAILABLE:
|
case TabManager.INDEX_AVAILABLE:
|
||||||
return availableFragment;
|
return new AvailableAppsFragment();
|
||||||
case TabManager.INDEX_INSTALLED:
|
case TabManager.INDEX_INSTALLED:
|
||||||
return installedFragment;
|
return new InstalledAppsFragment();
|
||||||
default:
|
default:
|
||||||
return canUpdateFragment;
|
return new CanUpdateAppsFragment();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -207,9 +207,11 @@ public abstract class AppListFragment extends ListFragment implements
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void updateSearchQuery(@Nullable String query) {
|
public void updateSearchQuery(@Nullable String query) {
|
||||||
searchQuery = query;
|
if (!TextUtils.equals(query, searchQuery)) {
|
||||||
if (isAdded()) {
|
searchQuery = query;
|
||||||
getLoaderManager().restartLoader(0, null, this);
|
if (isAdded()) {
|
||||||
|
getLoaderManager().restartLoader(0, null, this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user