Browse Source

Fixing fragment lifecycle for tab and bookmark drawers

master
anthony restaino 8 years ago
parent
commit
f90e897995
  1. 77
      app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java
  2. 36
      app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java
  3. 11
      app/src/main/java/acr/browser/lightning/fragment/TabsFragment.java

77
app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java

@ -170,7 +170,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
private ValueCallback<Uri> mUploadMessage; private ValueCallback<Uri> mUploadMessage;
private ValueCallback<Uri[]> mFilePathCallback; private ValueCallback<Uri[]> mFilePathCallback;
// Primatives // Primitives
private boolean mFullScreen; private boolean mFullScreen;
private boolean mDarkTheme; private boolean mDarkTheme;
private boolean mIsFullScreen = false; private boolean mIsFullScreen = false;
@ -292,20 +292,27 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
mWebpageBitmap = ThemeUtils.getThemedBitmap(this, R.drawable.ic_webpage, mDarkTheme); mWebpageBitmap = ThemeUtils.getThemedBitmap(this, R.drawable.ic_webpage, mDarkTheme);
final TabsFragment tabsFragment = new TabsFragment(); final FragmentManager fragmentManager = getSupportFragmentManager();
TabsFragment tabsFragment = (TabsFragment) fragmentManager.findFragmentByTag(TAG_TABS_FRAGMENT);
BookmarksFragment bookmarksFragment = (BookmarksFragment) fragmentManager.findFragmentByTag(TAG_BOOKMARK_FRAGMENT);
if (tabsFragment != null) {
fragmentManager.beginTransaction().remove(tabsFragment).commit();
}
tabsFragment = TabsFragment.createTabsFragment(isIncognito(), mShowTabsInDrawer);
mTabsView = tabsFragment; mTabsView = tabsFragment;
final Bundle tabsFragmentArguments = new Bundle();
tabsFragmentArguments.putBoolean(TabsFragment.IS_INCOGNITO, isIncognito());
tabsFragmentArguments.putBoolean(TabsFragment.VERTICAL_MODE, mShowTabsInDrawer);
tabsFragment.setArguments(tabsFragmentArguments);
final BookmarksFragment bookmarksFragment = new BookmarksFragment(); if (bookmarksFragment != null) {
fragmentManager.beginTransaction().remove(bookmarksFragment).commit();
}
bookmarksFragment = BookmarksFragment.createFragment(isIncognito());
mBookmarksView = bookmarksFragment; mBookmarksView = bookmarksFragment;
final Bundle bookmarksFragmentArguments = new Bundle();
bookmarksFragmentArguments.putBoolean(BookmarksFragment.INCOGNITO_MODE, isIncognito());
bookmarksFragment.setArguments(bookmarksFragmentArguments);
final FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.executePendingTransactions();
fragmentManager fragmentManager
.beginTransaction() .beginTransaction()
.replace(getTabsFragmentViewId(), tabsFragment, TAG_TABS_FRAGMENT) .replace(getTabsFragmentViewId(), tabsFragment, TAG_TABS_FRAGMENT)
@ -315,8 +322,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
mToolbarLayout.removeView(findViewById(R.id.tabs_toolbar_container)); mToolbarLayout.removeView(findViewById(R.id.tabs_toolbar_container));
} }
if (actionBar == null) Preconditions.checkNonNull(actionBar);
return;
// set display options of the ActionBar // set display options of the ActionBar
actionBar.setDisplayShowTitleEnabled(false); actionBar.setDisplayShowTitleEnabled(false);
@ -337,11 +343,35 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
mArrowImage.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); mArrowImage.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
} }
updateTabNumber(0); updateTabNumber(0);
// Post drawer locking in case the activity is being recreated
Handlers.MAIN.post(new Runnable() {
@Override
public void run() {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, getTabDrawer());
}
});
} else { } else {
// Post drawer locking in case the activity is being recreated
Handlers.MAIN.post(new Runnable() {
@Override
public void run() {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, getTabDrawer()); mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, getTabDrawer());
}
});
mArrowImage.setImageResource(R.drawable.ic_action_home); mArrowImage.setImageResource(R.drawable.ic_action_home);
mArrowImage.setColorFilter(mIconColor, PorterDuff.Mode.SRC_IN); mArrowImage.setColorFilter(mIconColor, PorterDuff.Mode.SRC_IN);
} }
// Post drawer locking in case the activity is being recreated
Handlers.MAIN.post(new Runnable() {
@Override
public void run() {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, getBookmarkDrawer());
}
});
arrowButton.setOnClickListener(this); arrowButton.setOnClickListener(this);
// create the search EditText in the ToolBar // create the search EditText in the ToolBar
@ -532,19 +562,25 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
@Override @Override
public void onDrawerClosed(View v) { public void onDrawerClosed(View v) {
if (v == mDrawerRight && mShowTabsInDrawer) { View tabsDrawer = getTabDrawer();
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, mDrawerLeft); View bookmarksDrawer = getBookmarkDrawer();
} else {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, mDrawerRight); if (v == tabsDrawer) {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, bookmarksDrawer);
} else if (mShowTabsInDrawer) {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, tabsDrawer);
} }
} }
@Override @Override
public void onDrawerOpened(View v) { public void onDrawerOpened(View v) {
if (v == mDrawerRight) { View tabsDrawer = getTabDrawer();
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, mDrawerLeft); View bookmarksDrawer = getBookmarkDrawer();
if (v == tabsDrawer) {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, bookmarksDrawer);
} else { } else {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, mDrawerRight); mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, tabsDrawer);
} }
} }
@ -612,7 +648,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
((BookmarksFragment) bookmarksFragment).reinitializePreferences(); ((BookmarksFragment) bookmarksFragment).reinitializePreferences();
} }
// TODO layout transition causing memory leak // TODO layout transition causing memory leak
// mBrowserFrame.setLayoutTransition(new LayoutTransition()); // mBrowserFrame.setLayoutTransition(new LayoutTransition());

36
app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java

@ -32,6 +32,7 @@ import com.anthonycr.bonsai.Single;
import com.anthonycr.bonsai.SingleAction; import com.anthonycr.bonsai.SingleAction;
import com.anthonycr.bonsai.SingleOnSubscribe; import com.anthonycr.bonsai.SingleOnSubscribe;
import com.anthonycr.bonsai.SingleSubscriber; import com.anthonycr.bonsai.SingleSubscriber;
import com.anthonycr.bonsai.Subscription;
import com.squareup.otto.Bus; import com.squareup.otto.Bus;
import com.squareup.otto.Subscribe; import com.squareup.otto.Subscribe;
@ -45,7 +46,6 @@ import acr.browser.lightning.R;
import acr.browser.lightning.activity.ReadingActivity; import acr.browser.lightning.activity.ReadingActivity;
import acr.browser.lightning.activity.TabsManager; import acr.browser.lightning.activity.TabsManager;
import acr.browser.lightning.app.BrowserApp; import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.favicon.FaviconModel;
import acr.browser.lightning.browser.BookmarksView; import acr.browser.lightning.browser.BookmarksView;
import acr.browser.lightning.bus.BookmarkEvents; import acr.browser.lightning.bus.BookmarkEvents;
import acr.browser.lightning.constant.Constants; import acr.browser.lightning.constant.Constants;
@ -53,6 +53,7 @@ import acr.browser.lightning.controller.UIController;
import acr.browser.lightning.database.BookmarkManager; import acr.browser.lightning.database.BookmarkManager;
import acr.browser.lightning.database.HistoryItem; import acr.browser.lightning.database.HistoryItem;
import acr.browser.lightning.dialog.LightningDialogBuilder; import acr.browser.lightning.dialog.LightningDialogBuilder;
import acr.browser.lightning.favicon.FaviconModel;
import acr.browser.lightning.preference.PreferenceManager; import acr.browser.lightning.preference.PreferenceManager;
import acr.browser.lightning.utils.ThemeUtils; import acr.browser.lightning.utils.ThemeUtils;
import acr.browser.lightning.view.LightningView; import acr.browser.lightning.view.LightningView;
@ -62,6 +63,16 @@ import butterknife.Unbinder;
public class BookmarksFragment extends Fragment implements View.OnClickListener, View.OnLongClickListener, BookmarksView { public class BookmarksFragment extends Fragment implements View.OnClickListener, View.OnLongClickListener, BookmarksView {
@NonNull
public static BookmarksFragment createFragment(boolean isIncognito) {
BookmarksFragment bookmarksFragment = new BookmarksFragment();
final Bundle bookmarksFragmentArguments = new Bundle();
bookmarksFragmentArguments.putBoolean(BookmarksFragment.INCOGNITO_MODE, isIncognito);
bookmarksFragment.setArguments(bookmarksFragmentArguments);
return bookmarksFragment;
}
private final static String TAG = BookmarksFragment.class.getSimpleName(); private final static String TAG = BookmarksFragment.class.getSimpleName();
public final static String INCOGNITO_MODE = TAG + ".INCOGNITO_MODE"; public final static String INCOGNITO_MODE = TAG + ".INCOGNITO_MODE";
@ -96,6 +107,8 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
@BindView(R.id.right_drawer_list) ListView mBookmarksListView; @BindView(R.id.right_drawer_list) ListView mBookmarksListView;
@BindView(R.id.starIcon) ImageView mBookmarkTitleImage; @BindView(R.id.starIcon) ImageView mBookmarkTitleImage;
@BindView(R.id.icon_star) ImageView mBookmarkImage; @BindView(R.id.icon_star) ImageView mBookmarkImage;
@Nullable
private Unbinder mUnbinder; private Unbinder mUnbinder;
// Colors // Colors
@ -103,6 +116,9 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
private boolean mIsIncognito; private boolean mIsIncognito;
@Nullable
private Subscription mBookmarksSubscription;
private Single<BookmarkViewAdapter> initBookmarkManager() { private Single<BookmarkViewAdapter> initBookmarkManager() {
return Single.create(new SingleAction<BookmarkViewAdapter>() { return Single.create(new SingleAction<BookmarkViewAdapter>() {
@Override @Override
@ -197,11 +213,12 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
setupNavigationButton(view, R.id.action_reading, R.id.icon_reading); setupNavigationButton(view, R.id.action_reading, R.id.icon_reading);
setupNavigationButton(view, R.id.action_toggle_desktop, R.id.icon_desktop); setupNavigationButton(view, R.id.action_toggle_desktop, R.id.icon_desktop);
initBookmarkManager().subscribeOn(Schedulers.io()) mBookmarksSubscription = initBookmarkManager().subscribeOn(Schedulers.io())
.observeOn(Schedulers.main()) .observeOn(Schedulers.main())
.subscribe(new SingleOnSubscribe<BookmarkViewAdapter>() { .subscribe(new SingleOnSubscribe<BookmarkViewAdapter>() {
@Override @Override
public void onItem(@Nullable BookmarkViewAdapter item) { public void onItem(@Nullable BookmarkViewAdapter item) {
mBookmarksSubscription = null;
mBookmarksListView.setAdapter(mBookmarkAdapter); mBookmarksListView.setAdapter(mBookmarkAdapter);
} }
}); });
@ -211,12 +228,25 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
@Override @Override
public void onDestroyView() { public void onDestroyView() {
super.onDestroyView(); super.onDestroyView();
if (mBookmarksSubscription != null) {
mBookmarksSubscription.unsubscribe();
mBookmarksSubscription = null;
}
if (mUnbinder != null) { if (mUnbinder != null) {
mUnbinder.unbind(); mUnbinder.unbind();
mUnbinder = null; mUnbinder = null;
} }
} }
@Override
public void onDestroy() {
super.onDestroy();
if (mBookmarksSubscription != null) {
mBookmarksSubscription.unsubscribe();
mBookmarksSubscription = null;
}
}
@Override @Override
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
@ -382,7 +412,7 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener,
final Context context; final Context context;
public BookmarkViewAdapter(Context context, @NonNull List<HistoryItem> data) { BookmarkViewAdapter(Context context, @NonNull List<HistoryItem> data) {
super(context, R.layout.bookmark_list_item, data); super(context, R.layout.bookmark_list_item, data);
this.context = context; this.context = context;
} }

11
app/src/main/java/acr/browser/lightning/fragment/TabsFragment.java

@ -61,6 +61,17 @@ import butterknife.Unbinder;
*/ */
public class TabsFragment extends Fragment implements View.OnClickListener, View.OnLongClickListener, TabsView { public class TabsFragment extends Fragment implements View.OnClickListener, View.OnLongClickListener, TabsView {
@NonNull
public static TabsFragment createTabsFragment(boolean isIncognito, boolean showTabsInDrawer) {
TabsFragment tabsFragment = new TabsFragment();
final Bundle tabsFragmentArguments = new Bundle();
tabsFragmentArguments.putBoolean(TabsFragment.IS_INCOGNITO, isIncognito);
tabsFragmentArguments.putBoolean(TabsFragment.VERTICAL_MODE, showTabsInDrawer);
tabsFragment.setArguments(tabsFragmentArguments);
return tabsFragment;
}
private static final String TAG = TabsFragment.class.getSimpleName(); private static final String TAG = TabsFragment.class.getSimpleName();
/** /**

Loading…
Cancel
Save