Fragments can't have constructors with args, refactor tlf instantiation into static helper

pull/14/head
Sam Bosley 13 years ago
parent be58f48af7
commit ce326d209a

@ -95,10 +95,6 @@ public class TagViewFragment extends TaskListFragment {
// --- UI initialization
public TagViewFragment(Bundle extras) {
super(extras);
}
@Override
public void onActivityCreated(final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

@ -64,10 +64,6 @@ public class GtasksListFragment extends DraggableTaskListFragment {
GtasksList.LAST_SYNC
};
public GtasksListFragment(Bundle extras) {
super(extras);
}
@Override
public void onActivityCreated(Bundle icicle) {
super.onActivityCreated(icicle);

@ -71,10 +71,6 @@ public class NotificationFragment extends TaskListFragment implements OnTimeSetL
private long taskId;
public NotificationFragment(Bundle extras) {
super(extras);
}
@Override
public void onCreate(Bundle savedInstanceState) {
StartupService.bypassInitialization();

@ -1,7 +1,5 @@
package com.todoroo.astrid.activity;
import java.lang.reflect.Constructor;
import android.app.PendingIntent.CanceledException;
import android.content.BroadcastReceiver;
import android.content.Context;
@ -168,7 +166,10 @@ public class AstridActivity extends FragmentActivity
setIntent(intent);
setupTasklistFragmentWithFilter(filter, (Bundle) intent.getExtras().clone());
Bundle extras = intent.getExtras();
if (extras != null)
extras = (Bundle) extras.clone();
setupTasklistFragmentWithFilter(filter, extras);
// no animation for dualpane-layout
AndroidUtilities.callOverridePendingTransition(this, 0, 0);
@ -204,20 +205,11 @@ public class AstridActivity extends FragmentActivity
}
protected final void setupTasklistFragmentWithFilterAndCustomTaskList(Filter filter, Bundle extras, Class<?> customTaskList) {
Class<?> component = customTaskList;
if (filter instanceof FilterWithCustomIntent) {
try {
component = Class.forName(((FilterWithCustomIntent) filter).customTaskList.getClassName());
} catch (Exception e) {
// Invalid
}
}
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
TaskListFragment newFragment = TaskListFragment.instantiateWithFilterAndExtras(filter, extras, customTaskList);
try {
Constructor<?> constructor = component.getConstructor(Bundle.class);
TaskListFragment newFragment = (TaskListFragment) constructor.newInstance(extras);
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.tasklist_fragment_container, newFragment,
TaskListFragment.TAG_TASKLIST_FRAGMENT);
transaction.commit();

@ -31,10 +31,6 @@ public class DraggableTaskListFragment extends TaskListFragment {
// --- task list
public DraggableTaskListFragment(Bundle extras) {
super(extras);
}
/**
* If database has an indent property for determining how rows are indented,
* return it here so we can read it from the cursor and use it

@ -147,7 +147,9 @@ public class TaskListActivity extends AstridActivity implements MainMenuListener
commentsButton.setOnClickListener(commentsButtonClickListener);
Filter savedFilter = getIntent().getParcelableExtra(TaskListFragment.TOKEN_FILTER);
Bundle extras = (Bundle) getIntent().getExtras().clone();
Bundle extras = getIntent().getExtras();
if (extras != null)
extras = (Bundle) extras.clone();
if (getIntent().getIntExtra(TOKEN_SOURCE, Constants.SOURCE_DEFAULT) == Constants.SOURCE_NOTIFICATION)
setupTasklistFragmentWithFilterAndCustomTaskList(savedFilter, extras, NotificationFragment.class);
else

@ -26,6 +26,7 @@ import android.support.v4.app.SupportActivity;
import android.support.v4.view.Menu;
import android.support.v4.view.MenuItem;
import android.text.TextUtils;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.KeyEvent;
@ -64,6 +65,7 @@ import com.todoroo.astrid.adapter.TaskAdapter.OnCompletedTaskListener;
import com.todoroo.astrid.adapter.TaskAdapter.ViewHolder;
import com.todoroo.astrid.api.AstridApiConstants;
import com.todoroo.astrid.api.Filter;
import com.todoroo.astrid.api.FilterWithCustomIntent;
import com.todoroo.astrid.api.TaskContextActionExposer;
import com.todoroo.astrid.api.TaskDecoration;
import com.todoroo.astrid.core.CoreFilterExposer;
@ -175,7 +177,7 @@ public class TaskListFragment extends ListFragment implements OnScrollListener,
private Timer backgroundTimer;
private boolean isFilter;
protected final Bundle extras;
protected Bundle extras;
private final TaskListContextMenuExtensionLoader contextMenuExtensionLoader = new TaskListContextMenuExtensionLoader();
@ -193,12 +195,31 @@ public class TaskListFragment extends ListFragment implements OnScrollListener,
AstridDependencyInjector.initialize();
}
private TaskListFragment() {
this(null);
@SuppressWarnings("nls")
public static TaskListFragment instantiateWithFilterAndExtras(Filter filter, Bundle extras, Class<?> customComponent) {
Class<?> component = customComponent;
if (filter instanceof FilterWithCustomIntent) {
try {
component = Class.forName(((FilterWithCustomIntent) filter).customTaskList.getClassName());
} catch (Exception e) {
// Invalid
}
}
TaskListFragment newFragment;
try {
newFragment = (TaskListFragment) component.newInstance();
} catch (java.lang.InstantiationException e) {
Log.e("tla-instantiate", "tla-instantiate", e);
newFragment = new TaskListFragment();
} catch (IllegalAccessException e) {
Log.e("tla-instantiate", "tla-instantiate", e);
newFragment = new TaskListFragment();
}
newFragment.setExtras(extras);
return newFragment;
}
public TaskListFragment(Bundle extras) {
super();
public void setExtras(Bundle extras) {
this.extras = extras;
}

@ -1,8 +1,6 @@
package com.todoroo.astrid.adapter;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
@ -31,25 +29,19 @@ public class TaskListFragmentPagerAdapter extends FragmentStatePagerAdapter {
}
private Fragment getFragmentForFilter(Filter filter) {
Bundle extras = getExtrasForFilter(filter);
return TaskListFragment.instantiateWithFilterAndExtras(filter, extras, TaskListFragment.class);
}
private Bundle getExtrasForFilter(Filter filter) {
Bundle extras;
if (filter instanceof FilterWithCustomIntent) {
try {
Class<?> component = Class.forName(((FilterWithCustomIntent) filter).customTaskList.getClassName());
Constructor<?> constructor = component.getConstructor(Boolean.class, Filter.class);
return (Fragment) constructor.newInstance(true, filter);
} catch (NoSuchMethodException e) {
return new TaskListFragment(null);
} catch (InvocationTargetException e) {
return new TaskListFragment(null);
} catch (ClassNotFoundException e) {
return new TaskListFragment(null);
} catch (IllegalAccessException e) {
return new TaskListFragment(null);
} catch (InstantiationException e) {
return new TaskListFragment(null);
}
extras = ((FilterWithCustomIntent) filter).customExtras;
} else {
return new TaskListFragment(null);
extras = new Bundle();
}
extras.putParcelable(TaskListFragment.TOKEN_FILTER, filter);
return extras;
}
}

Loading…
Cancel
Save