Load fragment used in last session correctly.

This commit is contained in:
Martin Fietz 2015-07-10 13:18:04 +02:00
parent 8516ca1267
commit b8b36b4ee8
2 changed files with 16 additions and 33 deletions

View File

@ -100,7 +100,6 @@ public class MainActivity extends ActionBarActivity implements NavDrawerActivity
private ActionBarDrawerToggle drawerToggle; private ActionBarDrawerToggle drawerToggle;
private CharSequence currentTitle; private CharSequence currentTitle;
private String currentFragment;
private ProgressDialog pd; private ProgressDialog pd;
@ -171,9 +170,9 @@ public class MainActivity extends ActionBarActivity implements NavDrawerActivity
String lastFragment = getLastNavFragment(); String lastFragment = getLastNavFragment();
if(ArrayUtils.contains(NAV_DRAWER_TAGS, lastFragment)) { if(ArrayUtils.contains(NAV_DRAWER_TAGS, lastFragment)) {
loadFragment(lastFragment, null); loadFragment(lastFragment, null);
} else {
loadFeedFragmentById(Integer.valueOf(lastFragment), null);
} }
// else: lastFragment contains feed id - drawer data is not loaded yet,
// so loading is postponed until then
} }
externalPlayerFragment = new ExternalPlayerFragment(); externalPlayerFragment = new ExternalPlayerFragment();
transaction.replace(R.id.playerFragment, externalPlayerFragment); transaction.replace(R.id.playerFragment, externalPlayerFragment);
@ -191,7 +190,6 @@ public class MainActivity extends ActionBarActivity implements NavDrawerActivity
} else { } else {
edit.remove(PREF_LAST_FRAGMENT_TAG); edit.remove(PREF_LAST_FRAGMENT_TAG);
} }
currentFragment = tag;
edit.commit(); edit.commit();
} }
@ -312,32 +310,20 @@ public class MainActivity extends ActionBarActivity implements NavDrawerActivity
return; return;
} }
Feed feed = itemAccess.getItem(relPos); Feed feed = itemAccess.getItem(relPos);
long feedId = feed.getId(); loadFeedFragmentById(feed.getId(), args);
}
public void loadFeedFragmentById(long feedId, Bundle args) {
Fragment fragment = ItemlistFragment.newInstance(feedId); Fragment fragment = ItemlistFragment.newInstance(feedId);
if(args != null) { if(args != null) {
fragment.setArguments(args); fragment.setArguments(args);
} }
saveLastNavFragment(String.valueOf(feed.getId())); saveLastNavFragment(String.valueOf(feedId));
currentTitle = ""; currentTitle = "";
getSupportActionBar().setTitle(currentTitle); getSupportActionBar().setTitle(currentTitle);
loadFragment(fragment); loadFragment(fragment);
} }
public void loadFeedFragmentById(long feedId) {
if (navDrawerData != null) {
int relPos = -1;
List<Feed> feeds = navDrawerData.feeds;
for (int i = 0; relPos < 0 && i < feeds.size(); i++) {
if (feeds.get(i).getId() == feedId) {
relPos = i;
}
}
if(relPos >= 0) {
loadFeedFragmentByPosition(relPos, null);
}
}
}
private void loadFragment(Fragment fragment) { private void loadFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager(); FragmentManager fragmentManager = getSupportFragmentManager();
// clear back stack // clear back stack
@ -377,15 +363,19 @@ public class MainActivity extends ActionBarActivity implements NavDrawerActivity
} }
private int getSelectedNavListIndex() { private int getSelectedNavListIndex() {
String lastFragment = getLastNavFragment(); String currentFragment = getLastNavFragment();
int tagIndex = navAdapter.getTags().indexOf(lastFragment); if(currentFragment == null) {
// should not happen, but better safe than sorry
return -1;
}
int tagIndex = navAdapter.getTags().indexOf(currentFragment);
if(tagIndex >= 0) { if(tagIndex >= 0) {
return tagIndex; return tagIndex;
} else if(ArrayUtils.contains(NAV_DRAWER_TAGS, lastFragment)) { } else if(ArrayUtils.contains(NAV_DRAWER_TAGS, currentFragment)) {
// the fragment was just hidden // the fragment was just hidden
return -1; return -1;
} else { // last fragment was not a list, but a feed } else { // last fragment was not a list, but a feed
long feedId = Long.parseLong(lastFragment); long feedId = Long.parseLong(currentFragment);
if (navDrawerData != null) { if (navDrawerData != null) {
List<Feed> feeds = navDrawerData.feeds; List<Feed> feeds = navDrawerData.feeds;
for (int i = 0; i < feeds.size(); i++) { for (int i = 0; i < feeds.size(); i++) {
@ -618,13 +608,6 @@ public class MainActivity extends ActionBarActivity implements NavDrawerActivity
navDrawerData = result; navDrawerData = result;
navAdapter.notifyDataSetChanged(); navAdapter.notifyDataSetChanged();
String lastFragment = getLastNavFragment();
if(currentFragment != lastFragment &&
!ArrayUtils.contains(NAV_DRAWER_TAGS, lastFragment)) {
long feedId = Long.valueOf(lastFragment);
loadFeedFragmentById(feedId);
}
if (handleIntent) { if (handleIntent) {
handleNavIntent(); handleNavIntent();
} }

View File

@ -120,7 +120,7 @@ public class SearchFragment extends ListFragment {
SearchResult result = (SearchResult) l.getAdapter().getItem(position); SearchResult result = (SearchResult) l.getAdapter().getItem(position);
FeedComponent comp = result.getComponent(); FeedComponent comp = result.getComponent();
if (comp.getClass() == Feed.class) { if (comp.getClass() == Feed.class) {
((MainActivity) getActivity()).loadFeedFragmentById(comp.getId()); ((MainActivity) getActivity()).loadFeedFragmentById(comp.getId(), null);
} else { } else {
if (comp.getClass() == FeedItem.class) { if (comp.getClass() == FeedItem.class) {
FeedItem item = (FeedItem) comp; FeedItem item = (FeedItem) comp;