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 @@ -170,7 +170,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
private ValueCallback<Uri> mUploadMessage;
private ValueCallback<Uri[]> mFilePathCallback;
// Primatives
// Primitives
private boolean mFullScreen;
private boolean mDarkTheme;
private boolean mIsFullScreen = false;
@ -292,20 +292,27 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -292,20 +292,27 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
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;
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;
final Bundle bookmarksFragmentArguments = new Bundle();
bookmarksFragmentArguments.putBoolean(BookmarksFragment.INCOGNITO_MODE, isIncognito());
bookmarksFragment.setArguments(bookmarksFragmentArguments);
final FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.executePendingTransactions();
fragmentManager
.beginTransaction()
.replace(getTabsFragmentViewId(), tabsFragment, TAG_TABS_FRAGMENT)
@ -315,8 +322,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -315,8 +322,7 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
mToolbarLayout.removeView(findViewById(R.id.tabs_toolbar_container));
}
if (actionBar == null)
return;
Preconditions.checkNonNull(actionBar);
// set display options of the ActionBar
actionBar.setDisplayShowTitleEnabled(false);
@ -337,11 +343,35 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -337,11 +343,35 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
mArrowImage.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
}
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 {
// 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());
}
});
mArrowImage.setImageResource(R.drawable.ic_action_home);
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);
// create the search EditText in the ToolBar
@ -532,19 +562,25 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -532,19 +562,25 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
@Override
public void onDrawerClosed(View v) {
if (v == mDrawerRight && mShowTabsInDrawer) {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, mDrawerLeft);
} else {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, mDrawerRight);
View tabsDrawer = getTabDrawer();
View bookmarksDrawer = getBookmarkDrawer();
if (v == tabsDrawer) {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, bookmarksDrawer);
} else if (mShowTabsInDrawer) {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, tabsDrawer);
}
}
@Override
public void onDrawerOpened(View v) {
if (v == mDrawerRight) {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, mDrawerLeft);
View tabsDrawer = getTabDrawer();
View bookmarksDrawer = getBookmarkDrawer();
if (v == tabsDrawer) {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, bookmarksDrawer);
} 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 @@ -612,7 +648,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
((BookmarksFragment) bookmarksFragment).reinitializePreferences();
}
// TODO layout transition causing memory leak
// mBrowserFrame.setLayoutTransition(new LayoutTransition());

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

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

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

@ -61,6 +61,17 @@ import butterknife.Unbinder; @@ -61,6 +61,17 @@ import butterknife.Unbinder;
*/
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();
/**

Loading…
Cancel
Save