From b79c666f22ecb796963aa29f49772920396f94a7 Mon Sep 17 00:00:00 2001 From: Tim Su Date: Tue, 24 Jan 2012 03:38:57 -0800 Subject: [PATCH] Cleaned up / consolidated fragment logic, implemented three-pane layout --- .../task_list_wrapper_activity.xml | 41 +++++++ .../task_list_wrapper_activity.xml | 12 +- .../activity/AstridWrapperActivity.java | 103 ++++++++++-------- .../astrid/activity/TaskEditActivity.java | 9 +- .../activity/TaskEditWrapperActivity.java | 5 +- .../activity/TaskListWrapperActivity.java | 52 ++++----- 6 files changed, 141 insertions(+), 81 deletions(-) create mode 100644 astrid/res/layout-large-land/task_list_wrapper_activity.xml diff --git a/astrid/res/layout-large-land/task_list_wrapper_activity.xml b/astrid/res/layout-large-land/task_list_wrapper_activity.xml new file mode 100644 index 000000000..78f34d5df --- /dev/null +++ b/astrid/res/layout-large-land/task_list_wrapper_activity.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/astrid/res/layout-large/task_list_wrapper_activity.xml b/astrid/res/layout-large/task_list_wrapper_activity.xml index 4cc724633..fdc694d06 100644 --- a/astrid/res/layout-large/task_list_wrapper_activity.xml +++ b/astrid/res/layout-large/task_list_wrapper_activity.xml @@ -4,21 +4,25 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> + + - + android:id="@+id/filterlist_fragment_container" /> + + + - + android:id="@+id/tasklist_fragment_container" /> + \ No newline at end of file diff --git a/astrid/src/com/todoroo/astrid/activity/AstridWrapperActivity.java b/astrid/src/com/todoroo/astrid/activity/AstridWrapperActivity.java index 4eb3b7494..219eaaabc 100644 --- a/astrid/src/com/todoroo/astrid/activity/AstridWrapperActivity.java +++ b/astrid/src/com/todoroo/astrid/activity/AstridWrapperActivity.java @@ -2,10 +2,11 @@ package com.todoroo.astrid.activity; import android.app.PendingIntent.CanceledException; import android.content.Intent; -import android.os.Bundle; +import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; +import android.view.View; import com.timsu.astrid.R; import com.todoroo.andlib.utility.AndroidUtilities; @@ -33,11 +34,11 @@ public class AstridWrapperActivity extends FragmentActivity TaskListActivity.OnTaskListItemClickedListener, TaskEditActivity.OnTaskEditDetailsClickedListener { - /** This flag shows if the landscape-multipane layouts are active. - * If a multipane-layout with two fragments is active, the callbacks implemented here - * should not start a new activity, but update the target-fragment directly instead. - */ - protected boolean mMultipleFragments = false; + public static final int LAYOUT_SINGLE = 0; + public static final int LAYOUT_DOUBLE = 1; + public static final int LAYOUT_TRIPLE = 2; + + protected int fragmentLayout = LAYOUT_SINGLE; public FilterListActivity getFilterListFragment() { FilterListActivity frag = (FilterListActivity) getSupportFragmentManager() @@ -60,23 +61,11 @@ public class AstridWrapperActivity extends FragmentActivity return frag; } - /* (non-Javadoc) - * @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle) - */ - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - } - - /* (non-Javadoc) - * @see android.app.Activity#onNewIntent(android.content.Intent) - */ @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); FilterListActivity frag = getFilterListFragment(); if (frag != null) { - // forwarding for search-requests frag.onNewIntent(intent); } } @@ -109,6 +98,7 @@ public class AstridWrapperActivity extends FragmentActivity setIntent(intent); setupTasklistFragmentWithFilter(filter); + // no animation for dualpane-layout AndroidUtilities.callOverridePendingTransition(this, 0, 0); StatisticsService.reportEvent(StatisticsConstants.FILTER_LIST); @@ -146,37 +136,15 @@ public class AstridWrapperActivity extends FragmentActivity } } - protected void setupFilterlistFragment() { - FragmentManager manager = getSupportFragmentManager(); - FragmentTransaction transaction = manager.beginTransaction(); - FilterListActivity newFragment = new FilterListActivity(); - if (findViewById(R.id.filterlist_fragment_container) != null) { - if (getFilterListFragment() != null) { - transaction.remove(getFilterListFragment()); - transaction.commit(); - transaction = manager.beginTransaction(); - } - transaction.replace(R.id.filterlist_fragment_container, newFragment, - FilterListActivity.TAG_FILTERLIST_FRAGMENT); - } else { - if (getFilterListFragment() != null) - return; - transaction.add(newFragment, FilterListActivity.TAG_FILTERLIST_FRAGMENT); - } - transaction.commit(); - } - - public boolean isMultipleFragments() { - return mMultipleFragments; - } - @Override public void onTaskListItemClicked(long taskId) { Intent intent = new Intent(this, TaskEditWrapperActivity.class); intent.putExtra(TaskEditActivity.TOKEN_ID, taskId); if (getIntent().hasExtra(TaskListActivity.TOKEN_FILTER)) intent.putExtra(TaskListActivity.TOKEN_FILTER, getIntent().getParcelableExtra(TaskListActivity.TOKEN_FILTER)); - if (this instanceof TaskEditWrapperActivity) { + + if (this instanceof TaskEditWrapperActivity || fragmentLayout == LAYOUT_TRIPLE) { + findViewById(R.id.taskedit_fragment_container).setVisibility(View.VISIBLE); TaskEditActivity editActivity = getTaskEditFragment(); editActivity.save(true); editActivity.repopulateFromScratch(intent); @@ -188,7 +156,54 @@ public class AstridWrapperActivity extends FragmentActivity @Override public void onTaskEditDetailsClicked(int category, int position) { - // TODO Auto-generated method stub + // + + } + + // --- fragment helpers + + protected void removeFragment(String tag) { + FragmentManager fm = getSupportFragmentManager(); + Fragment fragment = fm.findFragmentByTag(tag); + if(fragment != null) { + FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); + ft.remove(fragment); + ft.commit(); + } + } + protected void setupFragment(String tag, int container, Class cls) { + FragmentManager fm = getSupportFragmentManager(); + Fragment fragment = fm.findFragmentByTag(tag); + if(fragment == null) { + System.err.println("creating fragment of type " + cls.getSimpleName()); //$NON-NLS-1$ + try { + fragment = cls.newInstance(); + } catch (InstantiationException e) { + return; + } catch (IllegalAccessException e) { + return; + } + + FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); + ft.replace(container, fragment, tag); + ft.commit(); + } } + + /** + * @return LAYOUT_SINGLE, LAYOUT_DOUBLE, or LAYOUT_TRIPLE + */ + public int getFragmentLayout() { + return fragmentLayout; + } + + /** + * @deprecated please use the getFragmentLayout method instead + */ + @Deprecated + public boolean isMultipleFragments() { + return fragmentLayout != LAYOUT_SINGLE; + } + } diff --git a/astrid/src/com/todoroo/astrid/activity/TaskEditActivity.java b/astrid/src/com/todoroo/astrid/activity/TaskEditActivity.java index bee6cfe0d..428d2a242 100755 --- a/astrid/src/com/todoroo/astrid/activity/TaskEditActivity.java +++ b/astrid/src/com/todoroo/astrid/activity/TaskEditActivity.java @@ -253,9 +253,6 @@ public final class TaskEditActivity extends Fragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - // Tell the framework to try to keep this fragment around - // during a configuration change. - setRetainInstance(true); new StartupService().onStartupApplication(getActivity()); @@ -545,7 +542,11 @@ public final class TaskEditActivity extends Fragment { public void run() { AndroidUtilities.sleepDeep(500L); - getActivity().runOnUiThread(new Runnable() { + Activity activity = getActivity(); + if(activity == null) + return; + + activity.runOnUiThread(new Runnable() { public void run() { onUiThread(); } diff --git a/astrid/src/com/todoroo/astrid/activity/TaskEditWrapperActivity.java b/astrid/src/com/todoroo/astrid/activity/TaskEditWrapperActivity.java index 04cae2225..a286d3c9e 100644 --- a/astrid/src/com/todoroo/astrid/activity/TaskEditWrapperActivity.java +++ b/astrid/src/com/todoroo/astrid/activity/TaskEditWrapperActivity.java @@ -6,7 +6,6 @@ import android.support.v4.app.Fragment; import android.view.KeyEvent; import android.widget.TextView; -import com.timsu.astrid.R; import com.todoroo.andlib.utility.AndroidUtilities; import com.todoroo.astrid.service.ThemeService; @@ -39,9 +38,9 @@ public class TaskEditWrapperActivity extends AstridWrapperActivity { Fragment frag = (Fragment) getSupportFragmentManager() .findFragmentByTag(TaskListActivity.TAG_TASKLIST_FRAGMENT); if (frag != null) { - mMultipleFragments = true; + fragmentLayout = LAYOUT_DOUBLE; } else { - mMultipleFragments = false; + fragmentLayout = LAYOUT_SINGLE; } } diff --git a/astrid/src/com/todoroo/astrid/activity/TaskListWrapperActivity.java b/astrid/src/com/todoroo/astrid/activity/TaskListWrapperActivity.java index d1496356d..1087e6fc3 100644 --- a/astrid/src/com/todoroo/astrid/activity/TaskListWrapperActivity.java +++ b/astrid/src/com/todoroo/astrid/activity/TaskListWrapperActivity.java @@ -50,23 +50,41 @@ public class TaskListWrapperActivity extends AstridWrapperActivity { listsNav = actionBar.getCustomView().findViewById(R.id.lists_nav); lists = (TextView) actionBar.getCustomView().findViewById(R.id.list_title); - View container = findViewById(R.id.filterlist_fragment_container); - if (container != null) { - mMultipleFragments = true; + initializeFragments(actionBar); + } + + /** + * + * @param actionBar + */ + protected void initializeFragments(ActionBar actionBar) { + View filterFragment = findViewById(R.id.filterlist_fragment_container); + View editFragment = findViewById(R.id.taskedit_fragment_container); + + if (filterFragment != null) { actionBar.setDisplayHomeAsUpEnabled(false); actionBar.getCustomView().findViewById(R.id.list_disclosure_arrow).setVisibility(View.GONE); listsNav.setOnClickListener(null); + + if(editFragment != null) { + fragmentLayout = LAYOUT_TRIPLE; + setupFragment(TaskEditActivity.TAG_TASKEDIT_FRAGMENT, + R.id.taskedit_fragment_container, TaskEditActivity.class); + } else { + fragmentLayout = LAYOUT_DOUBLE; + } + + setupFragment(FilterListActivity.TAG_FILTERLIST_FRAGMENT, + R.id.filterlist_fragment_container, FilterListActivity.class); } else { - mMultipleFragments = false; + fragmentLayout = LAYOUT_SINGLE; actionBar.setDisplayHomeAsUpEnabled(true); listsNav.setOnClickListener(popupMenuClickListener); createPopover(); } - setupFilterlistFragment(); - - } + } - private void createPopover() { + private void createPopover() { popover = new ListDropdownPopover(TaskListWrapperActivity.this); popover.setOnDismissListener(new OnDismissListener() { @Override @@ -102,19 +120,6 @@ public class TaskListWrapperActivity extends AstridWrapperActivity { listsNav.setBackgroundColor(selected ? oldTextColor : android.R.color.transparent); } - /* (non-Javadoc) - * @see android.support.v4.app.FragmentActivity#onResume() - */ - - @Override - protected void onResume() { - super.onResume(); - FilterListActivity fla = getFilterListFragment(); - if (fla != null) { - fla.adapter.clear(); - } - } - @Override protected void onPostResume() { super.onPostResume(); @@ -132,11 +137,6 @@ public class TaskListWrapperActivity extends AstridWrapperActivity { popover.dismiss(); } - @Override - protected void onSaveInstanceState(Bundle icicle) { - super.onSaveInstanceState(icicle); - } - public void setSelectedItem(Filter item) { lists.setText(item.title); }